ImagePng Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

ImagePng Error

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That looks like an internal error in the extension.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read your private messages Justin.
Post Reply