简体   繁体   English

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. 我想知道如何自动找出最佳压缩率(小文件大小+无质量损失)。

At the moment I'm using imagejpeg() with $quality = 85 for each .jpg . 目前我正在使用每个.jpg $quality = 85 imagejpeg()

PageSpeed (Chrome Plugin) suggests, to lower the quality of a few images to save some kb. PageSpeed(Chrome插件)建议,降低一些图像的质量以节省一些kb。 The percentage of reduction is different. 减少的百分比是不同的。

I'd like to write a cronjob that crawls a specific directory and optimizes every image. 我想编写一个抓取特定目录并优化每个图像的cronjob。

How does PageSpeed or TinyPNG figure out the best optimized quality and is this possible with PHP or another serverside-language? PageSpeed或TinyPNG如何找出最佳的优化质量,这是否可以通过PHP或其他服务器语言实现?

TinyPNG uses pngquant . TinyPNG使用pngquant

Pngquant has option to set desired quality, similar to JPEG. Pngquant可以选择设置所需的质量,类似于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 . Pngquant网站提供了示例代码,展示了如何使用PHP中的pngquant


For JPEG you can apply lossless jpegcrush . 对于JPEG,您可以应用无损 jpegcrush

JpegMini (commercial) and jpeg-archive (free) are lossy and can can automatically find a minimal good quality for a JPEG. JpegMini (商业)和jpeg-archive (免费)是有损的 ,可以自动找到JPEG的最低质量。

In PHP you can roughly estimate how much JPEG was compressed by observing how much file size changes after re-compression. 在PHP中,您可以通过观察重新压缩后文件大小的变化来粗略估计压缩的JPEG数量。 File size of JPEG recompressed at same or higher quality will not change much (but will lose visual quality). 以相同或更高质量重新压缩的JPEG文件大小不会有太大变化(但会失去视觉质量)。

If you recompress JPEG and see file size halved, then keep the recompressed version. 如果重新压缩JPEG并查看文件大小减半,则保留重新压缩的版本。 If you see only 10-20% drop in file size, then keep the original. 如果您看到文件大小仅下降10-20%,则保留原始文件。

If you're compressing yourself, use MozJPEG (here's an online version ). 如果您正在压缩自己,请使用MozJPEG (这是在线版本 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM