Jump to content

Image upload clean up


acctman

Recommended Posts

can someone help me clean up this coding. the file extension, convert image and save image sections

 

if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["PHPSESSID"]);
}

session_start();

// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
	header("HTTP/1.1 500 Internal Server Error");
	echo "invalid upload";
	exit(0);
}

// Get File Extension
preg_match('/\.([^\.]+)$/',$_FILES["Filedata"]["tmp_name"],$out);
$ext = $out[1];  

// Convert Image to .jpg 
if($ext == "jpg" || $ext == "jpeg" || $ext == "pjpeg") {
          
          $image = imagecreatefromjpeg($picture); 
          
            } elseif($ext == "gif") {
            
          $image = imagecreatefromgif($picture); 

}

// Save converted image
header ("Content-type : image/jpg");
ImageJpeg($image);
ImageJpeg($image, "image.jpg");  // this needs to be $_FILES["Filedata"]["tmp_name"] . jpg
ImageDestroy($image);


// Get the image and create a thumbnail
$img = @imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$img) {
	header("HTTP/1.1 500 Internal Server Error");
	echo "could not create image handle";
	exit(0);
}

Link to comment
https://forums.phpfreaks.com/topic/103105-image-upload-clean-up/
Share on other sites

the coding for those 3 sections i just copied randomly from the net. so a lot of it doesn't work

 

Yeah, that happens a lot.

 

How about you start over by explaining what you want to do and what results you get from your randomly copied bits of code.  This isn't cleaning up, this is called writing a working script.

the coding for those 3 sections i just copied randomly from the net. so a lot of it doesn't work

 

Yeah, that happens a lot.

 

How about you start over by explaining what you want to do and what results you get from your randomly copied bits of code.  This isn't cleaning up, this is called writing a working script.

 

the problem i'm having is, i'm not getting any errors. But, it's not doing what its suppose to do, which is detect if the image is .gif and if it is convert it to .jpg and save the file with the new .jpg extension.

is it okay to use $_FILES["Filedata"]["tmp_name"] . jpg for the imagejpeg line at the bottom for saving?

 

<?php

// Get the session Id passed from Upload.
if (isset($_POST["PHPSESSID"])) {
	session_id($_POST["PHPSESSID"]);
}

session_start();

// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
	header("HTTP/1.1 500 Internal Server Error");
	echo "invalid upload";
	exit(0);
}


// Get File Extension
preg_match('/\.([^\.]+)$/',$_FILES["Filedata"]["tmp_name"],$out);
$ext = $out[1];  

// Convert Image to .jpg 
if($ext == "jpg" || $ext == "jpeg" || $ext == "pjpeg") {
          $image = imagecreatefromjpeg($picture); 
            } elseif($ext == "gif") {
          $image = imagecreatefromgif($picture); 
}

//Save converted image
header ("Content-type : image/jpg");
ImageJpeg($image);
ImageJpeg($image, "image.jpg");  ///////////// this needs to be $_FILES["Filedata"]["tmp_name"] . jpg
ImageDestroy($image);

i think the file extension part is working fine, after that comes the detection of for the ext. if its .jpg or .gif. if it gif then it should convert the image. the final part is the saving the image. I definitely know that this is not working . I made a comment next to the save part. I need for the tmp upload name to be saved with a .jpg extension.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.