Forum Moderators: coopster

Message Too Old, No Replies

GD Library PNG issues

I've tried JPEG resizing, but PNG is another story...

         

NogginAnimations

9:43 am on Aug 29, 2008 (gmt 0)

10+ Year Member



So I have this code, which works fine for JPEG and JPG files:

$SWFexten = findexts($_FILES['file']['name']);
$PNGexten = findexts($_FILES['prefile']['name']);

$targeSWF = "res/bin/art/art".$currentStamp.".".$SWFexten;
$targePNG = "res/bin/preview/temp".$currentStamp.".".$PNGexten;
$targeJPG = "res/bin/preview/art".$currentStamp.".jpg";

if(move_uploaded_file($_FILES['prefile']['tmp_name'],$targePNG)) {

$save = $targeJPG;
$file = $targePNG;
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($file);
$modwidth = 70;
$modheight = 70;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100);
unlink($targePNG);

}

but whenever I try something like this:


$SWFexten = findexts($_FILES['file']['name']);
$PNGexten = findexts($_FILES['prefile']['name']);
$targeSWF = "res/bin/art/art".$currentStamp.".".$SWFexten;
$targePNG = "res/bin/preview/temp".$currentStamp.".".$PNGexten;
$targeJPG = "res/bin/preview/art".$currentStamp.".png";

if(move_uploaded_file($_FILES['prefile']['tmp_name'],$targePNG)) {

$save = $targeJPG;
$file = $targePNG;
header('Content-type: image/png');
list($width, $height) = getimagesize($file);
$modwidth = 70;
$modheight = 70;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefrompng($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagepng($tn, $save, 100);
unlink($targePNG);

}

It throws errors at me..
What am I doing wrong?

PS: I want to do the same for .GIF files too (and I want to keep transparency)

[edited by: NogginAnimations at 9:50 am (utc) on Aug. 29, 2008]

whoisgregg

8:28 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One error popped out at me when I glanced through it... imagepng's third parameter [php.net] isn't a quality setting from 1 to 100 like JPEG, it's a compression level from 0 to 9 (with 0 meaning no compression).

And, in both cases, you don't need to send an output header if you are saving the output to disk... but, presumably, you are then using readfile or an additional image* call to output to the browser.

Other than that, it'd be a great help to know what errors it is actually returning. :)