PHP+Imagick - PNG Compression

心不动则不痛 提交于 2019-11-30 09:00:29

问题


How do I efficiently compress a PNG? In my case, the images are small grayscale images with transparency.

Currently I'm playing with this:

// ...

$im->setImageFormat('png');
$im->setImageColorspace(\Imagick::COLORSPACE_GRAY);
$im->setImageCompression(\Imagick::COMPRESSION_LZW);
$im->setImageCompressionQuality(9);
$im->stripImage();
$im->writeImage($url_t);

As Imagick doesn't offer COMPRESSION_PNG, I've tried LZW but there's almost no change in the filesize (usually it's even bigger than before).

If I open the image in GIMP and simply save it, the filesize gets drastically reduced (e.g. 11,341 B --> 3,763 B or 11,057 B --> 3,538).

What is the correct way of saving a compressed PNG with Imagick?


回答1:


Have a look at the first part of this answer:

  • Convert multipage PDF to PNG and back (Linux)

It explains the meaning + syntax of ImageMagick's -quality setting for PNGs.




回答2:


I'm definitely not sure if it is correct way to save PNG, but my way is:

$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);

This gives me perfect quality of the image and file size very similar to PS6 saved 'Save for Web'. Sometimes even smaller sizes!



来源:https://stackoverflow.com/questions/7462827/phpimagick-png-compression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!