I made a function to handle jpg and png files, but i get error when trying to upload a png file.
this is the function:
function createImg ($type, $src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
if ($type == "jpg/jpeg") {
//imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename.
$source = imagecreatefromjpeg($src);
}
else if ($type == "png") {
//imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
$source = imagecreatefrompng($src);
}
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,getWidth($src),getHeight($src));
if ($type == "jpg/jpeg") {
//imagejpeg() creates a JPEG file from the given image.
imagejpeg($newImage,$dst,$quality);
}
else if ($type == "png") {
//imagepng() creates a PNG file from the given image.
imagepng($newImage,$dst,$quality);
}
return $dst;
}
works as it should with jpg, but with png i get this error msg:
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib failed to initialize compressor -- stream error in E:...\php\functions.upload.php on line 48
Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in E:...\php\functions.upload.php on line 48
EDIT :
i just changed removed the imagepng(); and used only imagejpeg and it worked like this, i just want jpg files saved anyways. thanks!
How to Make a PNG? Fotor's online PNG maker offers an easy and fast way to generate PNG files. Upload your picture into Fotor to get started. Then click on the Background Remover tool and Fotor will automatically convert your image into a transparent PNG within seconds.
Upload your image to the online PNG maker and download it instantly. Choose an image, upload it to the remove background tool, and your new PNG file will be ready to download. Publish your new PNG image across all your social platforms or continue to edit it to perfection within Adobe Express.
The Adobe Express PNG converter is fast, free, and easy to use. Simply launch the converter, upload your image, and then instantly download your new PNG file.
The problem is because imagejpeg
quality can be up to 100, whereas imagepng
maximum quality is 9.
try this
else if ($type == "png") {
//imagepng() creates a PNG file from the given image.
$q=9/100;
$quality*=$q;
imagepng($newImage,$dst,$quality);
}
What value are you using for the quality setting? imagepng() uses values 0-9, whereas imagejpeg() uses 0-100.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With