Page 1 of 1

ImagePng Error

Posted: Tue Feb 06, 2007 1:42 pm
by JustinMs66
i have working image create thumbnail code for image uploads website.
but now what i want it to do is see what type of image it is, and if its a jpg, do imagejpg, if its a gif, do imagegif and if its a png do imagepng.
now so far this totally works. except for with PNG, it gives me this error:
Fatal error: imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png: fatal libpng error: zlib error in /website/file.php on line 155
so here is my code:

Code: Select all

//the uploaded file name:
$filename = basename( $_FILES['uploadedfile']['name']);

$width = 200;
$height = 200;

list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}
$image_p = imagecreatetruecolor($width, $height);

//get evrything into variables..
$newname2 = explode('.', $filename, 2);
$name1 = $newname2[0]; // if filename == "test1.jpg", will print: "test1"
$ext1 = $newname2[1]; // if filename == "test1.jpg", will print: "jpg"

//my if statements for image types
if ( $newname2[1] == "jpg" ) {
$image = imagecreatefromjpeg($filename);
}

if ( $newname2[1] == "png" ) {
$image = imagecreatefrompng($filename);

}

if ( $newname2[1] == "gif" ) {
$image = imagecreatefromgif($filename);
}

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

//now add the 1st part of the filename, plus the _thm, PLUS the extention
$newname3 = $name1 . "_thm" . "." . $ext1;


//code that actually saves it as a new file:

if ( $newname2[1] == "jpg" ) {
imagejpeg($image_p, $newname3, 100);
}

if ( $newname2[1] == "png" ) {
imagepng($image_p, $newname3, 100);
}

if ( $newname2[1] == "gif" ) {
imagegif($image_p, $newname3, 100);
}

and again, the GIF and JPG work perfectly. but not the PNG.
little help?

Posted: Tue Feb 06, 2007 5:32 pm
by feyd
That looks like an internal error in the extension.

Posted: Wed Feb 07, 2007 12:07 pm
by JustinMs66
thanks, but after hours of research i finally figured out the problem.

in this code:

Code: Select all

if ( $newname2[1] == "png" ) {
imagepng($image_p, $newname3, 100);
}
it turns out that PNG uses 1 to 10, not 1 to 100 for compression.
so i just changed the 100 to 9:

Code: Select all

if ( $newname2[1] == "png" ) {
imagepng($image_p, $newname3, 9);
}
and it works!
i am just posting this in case anyone searches up on this and needs the solution. so there it is.

Posted: Wed Feb 07, 2007 1:46 pm
by feyd
Read your private messages Justin.