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
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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.