Compress PNG files in PHP

I am creating a PNG file with a Cairo PHP extension. The image contains background and text. Now I want to compress these images using PHP after cairo has generated it. Is there a library for this?

I found the pngcrush tool. But this is a command line tool. I do not want to make a challenge system

. If there is no PHP solution, the C solution will do. In this case, I'll make a PHP extension.

I have read this related question. But there is no answer to that.


source to share

3 answers

I would look at PngOptimizer . You can get the source for it at the bottom of the page, and also have a separate CLI version.

The only problem is that the source is C ++ and not ANSI C. I've never done a PHP extension, so I don't know if that matters.



You can use imagepng () ...

//If you don't already have a handle to the image and it just on the file system...
$im = imagecreatefrompng("yourGenerateFile.png");
$quality = 5; //0 - 9 (0= no compression, 9 = high compression)
imagepng($im, 'file/to/save.png', $quality);  //leave out filename if you want it to output to the buffer




For C code take a look at ImageMagick . It looks like there is a PHP extension .



All Articles