且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载大图像时达到硬盘限制,加载时可能降采样吗?

更新时间:2023-12-05 16:19:16

如果要收缩JPEG图像,则imagemagick支持加载时收缩.例如,这是一个10k x 10k像素的JPEG图像,尺寸缩小到200x200.

If you are shrinking JPEG images, then imagemagick supports shrink-on-load. For example, here's a 10k x 10k pixel JPEG image being sized down to 200x200.

$ /usr/bin/time -f %M:%e \
    convert wtc.jpg -resize 200x200 x.jpg
713340:2.98

这是720MB的峰值内存使用量和将近3s的CPU时间.现在尝试这个:

That's 720MB of peak memory use and almost 3s of CPU time. Now try this:

$ /usr/bin/time -f %M:%e \
    convert -define jpeg:size=400x400 wtc.jpg -resize 200x200 x.jpg
35952:0.32

最多35MB的内存和300ms的CPU.

Down to 35MB of memory and 300ms of CPU.

-define jpeg:size = 400x400 提示JPEG加载器您需要至少400x400像素的图像,因此(在这种情况下)加载时,它将以1/第8个尺寸.您需要使加载提示大小至少比最终输出大小大2倍,以免发生混淆.

The -define jpeg:size=400x400 hints to the JPEG loader that you want an image of at least 400x400 pixels, so (in this case) during load, it'll fetch at 1/8th size. You need the load hint size to be at least 2x larger than your final output size to avoid aliasing.

您可以使用 setOption 从imagick进行设置.

You can set this from imagick with setOption.

不幸的是,许多装载机不支持装载收缩.PNG尤其糟糕:

Unfortunately, many loaders do not support shrink-on-load. PNG is especially bad:

$ /usr/bin/time -f %M:%e \
    convert wtc.png -resize 200x200 x.jpg
828376:5.62

830MB和5.6s.

830MB and 5.6s.

您可以考虑其他调整大小的程序. vipsthumbnail 很快和几乎所有文件格式的低内存,例如:

You could consider other resize programs. vipsthumbnail is fast and low-memory for almost all file formats, for example:

$ /usr/bin/time -f %M:%e \
    vipsthumbnail wtc.png --size 200x200 -o x.jpg
58780:2.29

相同的PNG文件

60MB和2.3s.质量与imagemagick相同.

60MB and 2.3s for the same PNG file. Quality is the same as imagemagick.

它也具有 PHP绑定-您可以编写例如:

It has a PHP binding too -- you can write eg.:

$image = Vips\Image::thumbnail('somefile.jpg', 200);
$image->writeToFile('tiny.jpg');