Jump to content

PHP-GD: corrupt png files


werty37

Recommended Posts

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;
}

Link to comment
https://forums.phpfreaks.com/topic/93844-php-gd-corrupt-png-files/
Share on other sites

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.

 

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); }

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); }

 

 

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.

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.