Jump to content

ImagePng Error


JustinMs66@hotmail.com

Recommended Posts

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:

 

 

//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?

Link to comment
https://forums.phpfreaks.com/topic/37342-imagepng-error/
Share on other sites

thanks, but after hours of research i finally figured out the problem.

 

in this code:

 

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:

 

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.

Link to comment
https://forums.phpfreaks.com/topic/37342-imagepng-error/#findComment-179301
Share on other sites

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.