2

I'm using Imagick and like to scale an image to a maximum file size of 2.5MB

I had a look to this SOF question: ImageMagick: scale JPEG image with a maximum file-size which is exactly what I want to do but the extent() method from Imagick does not have the size parameter: http://www.php.net/manual/en/imagick.extentimage.php

Anyone knows how I could to it? At the moment I'm trying to calculate a coefficient between the original file size and the target file size to calculate a new resolution but found out that the resolution is not proportional to the file size.

Update - The output format is always JPEG so if there is a way to calculate the size before to save it that would be great

Thanks in advance, Maxime

4

1 回答 1

3

您调用的范围函数只是设置图像的大小。

设置 jpeg 范围选项的功能是:

$imagick->setOption('jpeg:extent', '2500kb');

有趣的是,函数 $imagick->getImageBlob() 在设置此选项后似乎崩溃了。您被迫将文件写入磁盘,而不是能够直接获取它的字节。

输出格式始终为JPEG,因此如果有一种方法可以在保存之前计算大小,那就太好了

没有。对于给定的图像质量,每个图像中的细节量决定了图像在压缩后的大小。所以不可能计算出最终尺寸的质量水平。

来自底层 Image Magick 库的限制文件大小的 C 代码是:

maximum=101;
for (minimum=2; minimum < maximum; )
{
    jpeg_image->quality=minimum+(maximum-minimum+1)/2;
    status=WriteJPEGImage(jpeg_info,jpeg_image);
    if (GetBlobSize(jpeg_image) <= extent)
      minimum=jpeg_image->quality+1;
    else
      maximum=jpeg_image->quality-1;
    }
}

即它只是以不同的图像质量级别重新压缩文件,直到找到为给定值提供可接受文件大小的级别。

于 2013-10-28T15:52:01.100 回答