I'm trying to save a PNG image by copying from an other existing one, and using imagepng function, but the saved file has its size increased. Here is the initial image.
$destWidth = 1920;
$destHeight = 1080;
$srcWidth = 1920;
$srcHeight = 1080;
// im.png : 399,1 ko
// width : 1920
// heigth : 1080
$image = imagecreatefrompng('im.png');
$imageDest = imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($imageDest, $image, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
// imnew.png : 857,5 ko
imagepng($imageDest, 'imnew.png');
The initial image has 399,1 ko (with 1920x1080). But the result image has 857,5 ko (still 1920x1080). Why ? And is there any solution to not increase the size ?
There are many ways to store an image in a PNG file. The method used must differ between your original and the image saved by PHP. See:
https://www.w3.org/TR/2003/REC-PNG-20031110
If you look in the manual where imagepng() is documented you can see that PHP also has some options. For instance the $quality
parameter. This is one way of making the file smaller.
With imagetruecolortopalette() you can try to reduce the color depth of the image. This can also make the output file smaller.
Not all methods to make images smaller, that are supported by the PNG file format, are supported by PHP.