图像魔术库和GD库之间的图像文件大小差异


Image file size differences between Imagemagick and GD library

我一直在做一些测试,发现与GD库相比,Imagemagick可以创建更大的文件大小的图像。

我尝试使用缩略图方法和 Imagemagick 的调整图像大小方法(使用不同的过滤器(来创建最大尺寸为 1024x680 jpeg 的图像,JPEG 压缩和质量为 80,分辨率为每英寸 72 像素,并且我还使用 stripImage 方法来删除不需要的元数据。Imagemagick创建的文件大小始终在700KB到800KB的范围内,具体取决于各种过滤器。另一方面,GD 库生成的图像大小为 1024x680,大小仅为 41KB。

任何人都可以解释文件大小的差异。我在 Photo shop 中打开了 2 个文件并检查了任何差异,但找不到任何差异(DPI、颜色配置文件、8 位通道等(,但仍然无法解释文件大小的差异。

$srgbPath = "pathTosrgbColorProfile";
$srgb = file_get_contents($srgbPath);
$image->profileImage('icc', $srgb);
$image->stripImage();
$image->setImageResolution(72,72);
$image->setImageUnits(1);
$image->setInterlaceScheme(Imagick::INTERLACE_JPEG);
$image->setImageCompression(imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(80);
$image->setColorspace(imagick::COLORSPACE_SRGB);
$image->resizeImage($thumb_width,$thumb_nheight,imagick::FILTER_CATROM,1);
$image->writeImage($destination); 

大小减少了40KB,输出为711KB,这仍然相当大。我正在测试的高分辨率原始文件是大小为 3008x2000 (4.2MB( 的 jpeg。

编辑:

我想我想通了,该方法setCompression()为对象而不是图像执行此操作,而是我使用了setImageCompression()setImageCompressionQuality(),现在大小已减小到100KB。现在一切都好了!

也许GD和ImageMagick的质量设置不容易比较,一个的80%并不意味着另一个的80%相同。我在《粉碎杂志》的一篇文章中找到了以下注释:

事实证明,JPEG质量量表不是在规范或标准中定义的,并且在编码器之间也不统一。Photoshop 中 60 的质量可能与一个程序中的 40、另一个程序中的 B+ 质量和第三个程序中的质量幻想相同。在我的测试中,我发现Photoshop的60最接近ImageMagick中的-质量82。

因此,在比较不同的结果文件时,您可能会更加注意质量。也许颜色不同或gd图像有更多的伪影。

差异似乎很大; 几年前,当我做一些测试时,IM的文件大小大约是GD大小的5倍。看到使用的实际代码会很有趣。

我现在正在工作,但有一张照片调整为 592 x 592,文件大小为 50.3KB 我知道它的大小与您的大小不同,但它以 100 的质量保存

您可以运行以下命令并查看 IM 对输出文件的看法: 转换图像 -详细 -识别

编辑:

你一定做错了什么,我刚刚运行了一个测试,结果如下 - 由于某种原因,缩略图大小与调整大小相同!也许是一个错误。

原始文件大小:4700 x 3178 2.31MB

-调整大小尺寸 = 1021 x 680 186KB

-缩略图尺寸 = 1021 x 680 186KB

GD 尺寸 = 1024 x 682 100kb

$original = 'IMG_4979_1.CR2';
// Convert to jpg as GD will not work with CR2 files
exec("convert $original image.jpg");
$image = "image.jpg";
exec("convert $image -resize 1024x680 output1.jpg");
exec("convert $image -thumbnail 1024x680 -strip output2.jpg");
// Set the path to the image to resize
$input_image = 'image.jpg';
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Set the new width of the image
$thumb_width = "1024";
// Calculate the height of the new image to keep the aspect ratio
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] );
// Create a new true color image in the memory
$thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
// Create a new image from file 
$src_img = ImageCreateFromJPEG( $input_image );
// Create the resized image
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
// Save the image as resized.jpg
ImageJPEG( $thumbnail, "output3.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $thumbnail );