如果一个JPG图像只被编码一次,你实际上可以以一定的确定度检测它被压缩的质量。
你可以使用一个命令行工具叫做identify
来完成这个任务,它是Imagemagick的一部分(也可以作为PHP库使用,见下文)。例如,你可以运行以下命令:
identify -verbose 11397254.jpg
然后你会得到如下输出:
Image: 11397254.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 600x833+0+0
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Pixels: 499800
Red:
min: 7 (0.027451)
max: 251 (0.984314)
mean: 205.535 (0.80602)
standard deviation: 40.8098 (0.160038)
kurtosis: 7.3041
skewness: -2.81763
entropy: 0.627683
Green:
min: 4 (0.0156863)
max: 237 (0.929412)
mean: 200.186 (0.785044)
standard deviation: 45.9241 (0.180095)
kurtosis: 3.86968
skewness: -2.25667
entropy: 0.6339
Blue:
min: 0 (0)
max: 241 (0.945098)
mean: 191.701 (0.75177)
standard deviation: 54.6716 (0.214399)
kurtosis: 1.22795
skewness: -1.70134
entropy: 0.664107
Image statistics:
Overall:
min: 0 (0)
max: 251 (0.984314)
mean: 199.141 (0.780945)
standard deviation: 47.4814 (0.186202)
kurtosis: 3.61004
skewness: -2.23079
entropy: 0.641896
Rendering intent: Perceptual
Gamma: 0.454545
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: JPEG
Intensity: Undefined
Compose: Over
Page geometry: 600x833+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 100
Orientation: Undefined
Properties:
date:create: 2020-10-28T09:13:49+01:00
date:modify: 2020-10-28T09:13:45+01:00
jpeg:colorspace: 2
jpeg:sampling-factor: 2x2,1x1,1x1
signature: 7b5dc010915e4ae8b89891f7259a6f22efc69a31e5244403db580b70adc2ee94
Artifacts:
filename: 11397254.jpg
verbose: true
Tainted: False
Filesize: 147KB
Number pixels: 500K
Pixels per second: 49.98MB
User time: 0.010u
Elapsed time: 0:01.010
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org
重要的代码行在哪里:
Compression: JPEG
Quality: 100
如果质量为100,那么这张图片就没有被压缩过。
我用这个工具测试了GIMP的JPG压缩,它非常准确地检测了我在图像上使用的压缩百分比。
在PHP中,ImageMagick库中有一个函数:
https://www.php.net/manual/en/imagick.identifyimage.php
它似乎是同一工具的移植,你可以查看一下。