3

I am generating PNG file with cairo extension of PHP. The image contains a background and a text. Now I want to compress these images by PHP after its generated by cairo. Is there any library to do this?

I found pngcrush tool. But its a command line tool. I dont want to invoke system call. If there is not PHP solution a C solution would do. In that case I'll make a PHP extension.

I have read this related question. But there is no answer in it.

Community
  • 1
  • 1
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187

3 Answers3

3

You can use imagepng() ...

//If you don't already have a handle to the image and it's 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
imagedestroy($im);
Brad Harris
  • 1,520
  • 9
  • 12
1

I would take a look at PngOptimizer. You can get the source for it at the bottom of the page, and it has a separated CLI version too.

Only problem is that source is C++ , not ANSI C. I have never made a PHP extension, so i don't know if it makes a difference.

tereško
  • 58,060
  • 25
  • 98
  • 150
0

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

RunHolt
  • 1,892
  • 2
  • 19
  • 26
  • Read http://stackoverflow.com/questions/7462827/phpimagick-png-compression it does not work that way. – Shiplu Mokaddim Mar 30 '12 at 17:45
  • Aha! It looks like a PHP problem not an ImageMagick issue. You could either add the flag to the PHP-ImageMagick extension or create your own. [MagickCore](http://www.imagemagick.org/script/magick-core.php) has an example (search for 'example') of how to convert a gif to a png in code. – RunHolt Mar 30 '12 at 17:54
  • If you see the related question, there OP tried it with PHP but I tried in command line too. in both case I achieve no more than 10% compression. – Shiplu Mokaddim Mar 31 '12 at 03:16