werty37 Posted March 1, 2008 Share Posted March 1, 2008 Hi I am trying to resize an uploaded image and save it as png. I always get corrupted png thumbnails. Can somebody check the function and tell where i ve gone wrong. function image_thumb($file, $tnfilename) { //maximum width: 225px //maximum height 225px $imageInfo = getimagesize($file); if ($imageInfo["mime"] == 'image/jpeg') $fullImage = imagecreatefromjpeg($file); elseif ($imageInfo["mime"] == 'image/png') $fullImage = imagecreatefrompng($file); $imageWidth = $imageInfo[0]; $imageHeight = $imageInfo[1]; $imageRatio = $imageWidth/$imageHeight; if ($imageRatio < 1) { $newHeight = 225; $newWidth = (225/$imageHeight) * $imageWidth; echo $newWidth;} else { $newWidth = 225; $newHeight = (225/$imageWidth) * $imageHeight;} $tnImage = imagecreatetruecolor($newWidth, $newHeight); imagealphablending($tnImage, true); imagecopyresampled($tnImage, $fullImage, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight); imagesavealpha($tnImage, true); imagepng($tnImage, $tnfilename, 70, PNG_ALL_FILTERS); imagedestroy($fullImage); imagedestroy($tnImage); return $tnfilename; } Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 hmmm at the top of the script your checking to see what kind of file it is eg. jpeg or png if ($imageInfo["mime"] == 'image/jpeg') $fullImage = imagecreatefromjpeg($file); elseif ($imageInfo["mime"] == 'image/png') $fullImage = imagecreatefrompng($file); at the bottom of the script ur just presuming that its png? imagepng($tnImage, $tnfilename, 70, PNG_ALL_FILTERS); might be wrong dunno never read through all ur script. Quote Link to comment Share on other sites More sharing options...
werty37 Posted March 1, 2008 Author Share Posted March 1, 2008 Hi ILYAS415, I am trying to create png thumbnails from jpg or png images. Hope the code now makes sense Thanks Quote Link to comment Share on other sites More sharing options...
JasonO Posted March 1, 2008 Share Posted March 1, 2008 Check for spaces outside the PHP area. Spaces in the wrong places can corrupt an image - I learnt the hard way Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 try changing this line if ($imageInfo["mime"] == 'image/jpeg') $fullImage = imagecreatefromjpeg($file); elseif ($imageInfo["mime"] == 'image/png') $fullImage = imagecreatefrompng($file); to... if ($imageInfo["mime"] == 'image/jpeg'){ $fullImage = imagecreatefromjpeg($file); }elseif ($imageInfo["mime"] == 'image/png') $fullImage = imagecreatefrompng($file); } Quote Link to comment Share on other sites More sharing options...
werty37 Posted March 2, 2008 Author Share Posted March 2, 2008 Hi There are no spaces around <?php ?> tags.... Check for spaces outside the PHP area. Spaces in the wrong places can corrupt an image - I learnt the hard way Didnt help try changing this line if ($imageInfo["mime"] == 'image/jpeg') $fullImage = imagecreatefromjpeg($file); elseif ($imageInfo["mime"] == 'image/png') $fullImage = imagecreatefrompng($file); to... if ($imageInfo["mime"] == 'image/jpeg'){ $fullImage = imagecreatefromjpeg($file); }elseif ($imageInfo["mime"] == 'image/png') $fullImage = imagecreatefrompng($file); } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 2, 2008 Share Posted March 2, 2008 Add the following two lines of code after your first opening <?php tag to see if any errors are occurring - ini_set ("display_errors", "1"); error_reporting(E_ALL); The code you posted gave me two warning messages due to the options/flags used in the imagepng() function. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 3, 2008 Share Posted March 3, 2008 The reason why the posted code results in an error is because the 3rd parameter in the imagepng() function is a 0-9 value, so the 70 used in the code is invalid - quality Compression level: from 0 (no compression) to 9. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.