检测JPG压缩率?
通常,当图像进入我的网站时,我使用我编写的图像库将其保存为 jpg,默认质量为 80%。现在,当我需要对其进行其他操作(例如裁剪,甚至只是调整其大小)时,图像将以 jpg 格式打开、处理,然后保存回来。但是,如果之前已经压缩过,我不想再压缩它,否则每次需要执行操作时质量都会下降。
有没有一种方法可以使用标准 GD php 库中的工具检测图像之前已经被压缩了多少(我猜与它的 png 版本相比)?我知道检测图像经过 Photoshop 处理的位置的工具是通过比较相对压缩量来实现的,因此我认为可以检测压缩量,但有人知道我将如何进行此计算吗?谢谢。
Normally when an image comes into my site I save it as jpg using an image library I wrote with the default quality of 80%. Now when I need to do some other operation on it (like cropping it, or even just resizing it) the image will be opened as jpg, processed, then saved back. However, if it has been compressed before I don't want to compress it again or else every time I need to do an operation the quality will fall.
Is there a way that I can detect how much the image has already been compressed before (in comparison to a png version of it I guess) using tools in the standard GD php libraries? I know that tools which detect where an image has been Photoshopped do so by comparing relative amounts of compression so I would think it is possible to detect the amount of compression but does anyone know how I would go about doing this calculation? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法从 JPG 中获得质量价值。此外,质量值取决于编码器。它不是标准或类似的东西。有些程序只有(低、中、高),有些 20 可能比 90 更好。
其次,JPG 在每次后续编码中都会损失质量,即使您每次都将其保存为最佳质量。生活中的悲惨事实:)唯一不会降低质量的操作是翻转和旋转(以及裁剪,如果它们与 JPEG 块大小对齐)。
经验法则:每次都以相同的质量值进行编码。示例:如果您以 60 保存一次,那么如果下次以 80 保存,则不会有任何增益。只是文件大小更大。
另外,如果您有足够的可用存储空间,请尝试减少此类重新编码的数量并在原始 上执行每个操作。
You cannot get quality value from JPG. Moreover, the quality value is encoder dependent. It is not a standard or anything like this. Some programs have only (low, medium, high), in some 20 might be better than 90.
Secondly, JPG simply will lose quality in each cosnequent encoding, even if you save it as best quality every time. Sad truth of life :) The only operations that do not worse the quality are flips and rotations (and crops, if they are aligned to JPEG block size).
Rule of the thumb: Encode it every time at the same quality value. Example: If you save it once at let's say 60, then there is no gain if you save it at 80 next time. Just bigger file size.
Also, try to reduce the number of such re-encodings and perform each manipulation on the original , if you have enough storage available.
您必须将图像的质量存储在数据库中,以便您知道它是否已经被压缩。
You will have to store the image's quality in a database so you can know if it has already been compressed or not.
为了避免多次压缩图像,您可以简单地将调整后的文件大小与原始文件大小进行比较。
此外,由于以相同的速率重新压缩文件只会减少文件大小的一小部分,因此如果您使用整数作为速率(60%、70%、80%等),则可以确定调整大小的文件的速率尺寸与原件非常相似。
例如,压缩
1,844 KB
文件:-
90%
=2,115 KB
。尺寸增加了,所以我不会使用它。-
80%
=1,843 KB
。这与原始文件大小几乎相同,因此我可以假设原始文件的比率为80%
。-
70%
=1,567 KB
。这个已经压缩了,所以我会用它。最后,如果您只关心自己压缩的图像的速率,那么您可以使用 PHP 来保存文件元数据中使用的速率。
To avoid compressing the image multiple times you could simply compare the file size of the resize with the original.
Also, as recompressing a file with the same rate only shaves a small amount off of the file size, if you use round numbers for your rates (60%, 70%, 80% etc.) you could ascertain the rate if the resize's file size is very similar to the original's.
For example, compressing a
1,844 KB
file at:-
90%
=2,115 KB
. The size increased, so I won't use it.-
80%
=1,843 KB
. This is almost identical to the original's file size so I can assume that the original has an80%
rate.-
70%
=1,567 KB
. This has compressed, so I'll use it.Finally, if you're only concerned with the rates of images that you've compressed yourself, then you could use PHP to save the rate you use in the file's metadata.
如果 JPG 图像仅被编码一次,您实际上可以检测 JPG 图像的压缩质量,并且具有一定的确定性。
您可以使用名为
identify
的命令行工具来完成此操作,该工具是 Imagemagick 的一部分(也可以作为 PHP 库提供,请参见下文)。例如,您可以运行:identify -verbose 11397254.jpg
,您将得到如下输出:
重要的几行是:
如果质量为 100,则意味着图像尚未压缩根本不。
我针对 GIMP JPG 压缩测试了该工具,它在检测图像上使用的压缩百分比方面非常准确。
在 PHP 中,ImageMagick 库中有一个函数:
https://www.php .net/manual/en/imagick.identifyimage.php
这似乎是同一工具的移植,所以你可以检查一下。
You actually can detect the quality at which a JPG image has been compressed, with some degree of certainty, if it has been encoded only once.
You can do it with a command-line tool called
identify
, which is part of Imagemagick (which is also available as a PHP library, see below). You can run, for example:identify -verbose 11397254.jpg
and you will get an output like this:
Where the important lines are:
If quality is 100, it means that the image hasn't been compressed at all.
I tested this tool against GIMP JPG compression, and it was very accurate in detecting the percentage of compression I used on images.
In PHP, there's a function in the ImageMagick library:
https://www.php.net/manual/en/imagick.identifyimage.php
It seems to be a porting of the same tool, so you could check that.