gpt4 book ai didi

php - 检测 JPEG 图像质量

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:48 28 4
gpt4 key购买 nike

我允许用户上传图片。但是,我想保持 JPEG 质量不超过 90%。我打算做的是检测当前质量:- 如果少于 90% 什么都不做- 如果超过 90%,则使用 Image Magick 将图像重新压缩到 90%

这有可能吗?我更喜欢 PHP,但任何语言都会有所帮助。

最佳答案

paddy 是正确的,此设置并不总是存储在 JPEG 文件中。如果是,那么您可以使用 Imagemagick 中的 identify 来读取质量。例如:

$ identify -format '%Q' tornado_ok.jpg

93%

Update: Based on the answer to this question https://superuser.com/questions/62730/how-to-find-the-jpg-quality I find out that apparently the identify command can still determine the quality by reverse engineering the quantization tables even if all the image EXIF / other meta data is lost. By the way, the title of your question as it stands now is a possible duplicate of that question I linked to.

But to me your question has merit on its own because in your question's text you explain what you are trying to do, which is more than simply detecting jpeg quality. Nevertheless, you should perhaps update the title if you want to reflect that you are trying to solve a more specific problem than just reading JPEG image quality.

除非您要存档原始图像,否则对于网络使用,即使 90% 也过多。在过去,75% 曾经是默认值(只有在并排图像之间仔细检查时才能看到质量下降),而现在在高带宽时代,85% 是一个非常高质量的选项。 90% 和 85% 之间的 5% 质量差异几乎是看不见的,但通常会为您节省超过 30% 的文件大小。 JPEG 算法的设计初衷是在其第一个压缩阶段(大约 80% 以上)消除人类感知不可见的信息。

Update/note: The compression quality settings I am talking about are from tests with libjpeg, a very widely used JPEG library. Photoshop's compression percentages and other software's quality settings are all independent and do not necessarily mean the same thing as the settings of libjpeg.

paddy 使用图像高度和图像宽度来计算可接受的文件大小的想法是合理的:

你可以像这样获取图像的高度/宽度:

list($originalWidth, $originalHeight) = getimagesize($imageFile);

我自己在网上发布的高质量照片,比如这张:http://ksathletics.com/2013/wsumbb/nw.jpg通常以每百万像素约 200 KB 的比例保存。

例如,您可以将宽度乘以高度再除以 1000000 来计算图像中的百万像素。将文件大小除以 1024 以计算 KB。然后将得到的 KB 除以百万像素。如果结果小于 200 或您决定的任何值,则无需重新压缩。否则,您可以使用 85% 的质量或您决定的任何质量重新压缩它。

关于php - 检测 JPEG 图像质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738276/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com