PHP: How to compress images without losing visible quality (automatically)?

强颜欢笑 提交于 2019-11-28 17:18:28

TinyPNG uses pngquant.

Pngquant has option to set desired quality, similar to JPEG. You can run something like:

<?php system('pngquant --quality=85 image.png'); ?>

Pngquant website has example code showing how to use pngquant from PHP.


For JPEG you can apply lossless jpegcrush.

JpegMini (commercial) and jpeg-archive (free) are lossy and can can automatically find a minimal good quality for a JPEG.

In PHP you can roughly estimate how much JPEG was compressed by observing how much file size changes after re-compression. File size of JPEG recompressed at same or higher quality will not change much (but will lose visual quality).

If you recompress JPEG and see file size halved, then keep the recompressed version. If you see only 10-20% drop in file size, then keep the original.

If you're compressing yourself, use MozJPEG (here's an online version).

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