Jump to content

ImagePng Error


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