OriginalUtter
OriginalUtter

Reputation: 669

PHP: imagepng() creates broken image

I have a problem creating a thumnail of a PNG image.

This works:

    $src = imagecreatefromjpeg("http://www.gaze.se/".$folder.$filename);
    $rootfolder = $_SERVER['DOCUMENT_ROOT'] ;
    $tmp = imagecreatetruecolor($targ_w, $targ_h);
    imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);
    imagejpeg($tmp, $rootfolder.$folder.'thumb_'.$filename,100);

    imagedestroy($tmp);
    imagedestroy($src);

Then I change the two jpeg functions to png, like this:

    $src = imagecreatefrompng("http://www.gaze.se/".$folder.$filename);
    $rootfolder = $_SERVER['DOCUMENT_ROOT'] ;
    $tmp = imagecreatetruecolor($targ_w, $targ_h);
    imagecopyresampled($tmp, $src, 0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);
    imagepng($tmp, $rootfolder.$folder.'thumb_'.$filename,100);

    imagedestroy($tmp);
    imagedestroy($src);

But then the image is broken, this message shows when i try to open it:

Error message

Any ideas of how to solve this?

Upvotes: 0

Views: 2031

Answers (1)

wldsvc
wldsvc

Reputation: 1272

Check out http://php.net/manual/en/function.imagepng.php. The $quality parameter should be 0 to 9 for PNG files. And as @jeroen said, you should definitely check the return values.

Upvotes: 8

Related Questions