I have this script I want to use but it only works with images with extensions .jpg. What should I do to the script so it works with images like .gif, .png, ect. Here's the script... <?php // This is the temporary file created by PHP $uploadedfile = $_FILES['uploadfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); $newwidth=164; //width of the new image. $newheight=($height/$width)*158; //height of the new image. $tmp=imagecreatetruecolor($newwidth,$newheight); //Resize the image. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // Save the new image with a different name. $filename = $_FILES['uploadfile']['name']; if (imagejpeg($tmp,$filename,100)) { echo 'Picture was uploaded successfully.'; } imagedestroy($src); imagedestroy($tmp); ? PHP: > Thanks in advance.
This line: $src = imagecreatefromjpeg($uploadedfile); PHP: Needs to be adjusted for each filetype. Such other options are: http://php.net/imagecreatefrompng http://php.net/imagecreatefromgif Jay
Hi, Jayshah was right, you need to change the function names - But if you want to keep the same file type as what was uploaded, you must perform a check to detect the current image type. for example checking the extension; $pathExt = pathinfo($_FILES['uploadfile']['tmp_name']); if($pathExt["extension"] == "gif") { //Do imagecreatefromgif function } elseif($pathExt["extension"] == "jpg") { //Do imagecreatefromjpeg function } elseif($pathExt["extension"] == "png") { //Do imagecreatefrompng function } else { //Not a supported filetype } PHP: Regards, Steve
Thanks You can also use the (deprecated) mime_content_type or the newer FileInfo to detect the true filetype without relying on the extension. Jay
Thank you guys! , but I try that and it gave me some errors. Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\Test\test1\upload.php on line 33 Line 33: imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\wamp\www\Test\test1\upload.php on line 44 Line 44: imagedestroy($src); And the script updated... <?php // This is the temporary file created by PHP $uploadedfile = $_FILES['uploadfile']['tmp_name']; $pathExt = pathinfo($uploadedfile); if($pathExt["extension"] == "gif") { $src = imagecreatefromgif($uploadedfile); } elseif($pathExt["extension"] == "jpg") { $src = imagecreatefromjpeg($uploadedfile); } elseif($pathExt["extension"] == "png") { $src= imagecreatefrompng($uploadedfile); } else { echo 'Sorry the image you\'re tying to upload it\'s not allowed.'; } // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); $newwidth=164; //width of the new image. $newheight=($height/$width)*158; //height of the new image. $tmp=imagecreatetruecolor($newwidth,$newheight); //Resize the image. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // Save the new image with a different name. $new_name = $_FILES['uploadfile']['name']; $filename = "images/$new_name"; if (imagejpeg($tmp,$filename,100)) { echo 'Picture was uploaded successfully.'; } imagedestroy($src); imagedestroy($tmp); ?> PHP:
That means that your imagecreatefrom is not working properly. imagecopyresampled is not getting anything.