PHP:如何压缩图像而不会失去可见质量(自动)? [英] PHP: How to compress images without losing visible quality (automatically)?
问题描述
我想知道如何自动找出最佳压缩率(小文件大小+无质量损失)。
I'm wondering how to figure out the best compress rate (small filesize + no quality loss) automatically.
目前我使用 imagejpeg()每个 .jpg
$ quality = 85
PageSpeed(Chrome插件)建议,降低几幅图片的质量以节省一些kb。减少的百分比是不同的。
PageSpeed (Chrome Plugin) suggests, to lower the quality of a few images to save some kb. The percentage of reduction is different.
我想编写一个抓取特定目录并优化每个图像的cronjob。
I'd like to write a cronjob that crawls a specific directory and optimizes every image.
PageSpeed或 TinyPNG 找出最佳优化质量,这是否可以使用PHP或其他服务器语言?
How does PageSpeed or TinyPNG figure out the best optimized quality and is this possible with PHP or another serverside-language?
推荐答案
TinyPNG使用 pngquant 。
TinyPNG uses pngquant.
Pngquant可以选择设置所需的质量,类似于JPEG。您可以运行像:
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.
对于JPEG,您可以应用 lossless jpegcrush 。
For JPEG you can apply lossless jpegcrush.
JpegMini (商业版)和 jpeg-archive (免费)是有损的
JpegMini (commercial) and jpeg-archive (free) are lossy and can can automatically find a minimal good quality for a JPEG.
在PHP中,您可以通过观察文件大小的变化来粗略估计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).
如果您重新压缩JPEG并查看文件大小减半,则保留再压缩版本。如果您只看到文件大小减少10-20%,请保留原始文件。
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.
如果您正在压缩自己,请使用 MozJPEG (以下是在线版)。
If you're compressing yourself, use MozJPEG (here's an online version).
这篇关于PHP:如何压缩图像而不会失去可见质量(自动)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!