Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating png files

Tags:

php

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!

like image 975
Alexander Avatar asked Oct 24 '11 16:10

Alexander


People also ask

How do you create a PNG file?

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.

How do I make a PNG file free?

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.

Can I convert an image to PNG?

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.


2 Answers

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);      
}
like image 69
Johnny Craig Avatar answered Oct 18 '22 04:10

Johnny Craig


What value are you using for the quality setting? imagepng() uses values 0-9, whereas imagejpeg() uses 0-100.

like image 45
StuR Avatar answered Oct 18 '22 04:10

StuR