ImageMagick是叠加图像的最快方法吗?如何提高速度,或者我不知道有没有更快的技术?

Wya*_*son 2 gd image-manipulation imagemagick graphicsmagick

我正在构建一个图像处理Nginx CDN /缓存服务器,以在服装jpeg上覆盖数百万个独特的SVG设计文件。此处类似的教程:http//sumitbirla.com/2011/11/how-to-build-a-scalable-caching-resizing-image-server/

我在这里写了一个测试脚本:

<?php

$cmd = "composite GOSHEN.svg blank-tshirt.jpg -geometry 600x700+456+335 JPG:-";

header("Content-type: image/jpeg");
passthru($cmd);
exit();

?>
Run Code Online (Sandbox Code Playgroud)

这是一个示例结果:

我的问题是ImageMagick太慢了。除了更多的CPU /内存,还有什么技巧可以使其更快?是否有其他替代技术可以更快地覆盖图像?

任何帮助深表感谢。

jcu*_*itt 5

php-vips可以比imagick更快。我为您制作了一个测试程序:

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;

for($i = 0; $i < 100; $i++) {
    $base = Vips\Image::newFromFile($argv[1], ["access" => "sequential"]);
    $overlay = Vips\Image::newFromFile($argv[2], ["access" => "sequential"]);

    // centre the overlay on the image, but lift it up a bit    
    $left = ($base->width - $overlay->width) * 0.5;
    $top = ($base->height - $overlay->height) * 0.45;

    $out = $base->composite2($overlay, "over", ["x" => $left, "y" => $top]);

    // write to stdout with a mime header
    $out->jpegsave_mime();
}       
Run Code Online (Sandbox Code Playgroud)

使用服务器中的测试图像:

http://build9.hometownapparel.com/pics/

然后在我的台式机(Ubuntu 17.10,快速的i7 CPU)上运行,我看到:

$ time ./overlay.php blank-tshirt.jpg GOSHEN.svg > /dev/null
real    0m2.488s
user    0m13.446s
sys 0m0.328s
Run Code Online (Sandbox Code Playgroud)

因此,每张图片大约需要25ms。我看到了这个结果(显然是从第一次迭代中得出的):

样本输出

我尝试了您的imagemagick示例的循环版本:

#!/usr/bin/env php
<?php

header("Content-type: image/jpeg");

for($i = 0; $i < 100; $i++) {
    $cmd = "composite GOSHEN.svg blank-tshirt.jpg -geometry 600x700+456+335 JPG:-";

    passthru($cmd);
}     
Run Code Online (Sandbox Code Playgroud)

针对IM-6.9.7-4(为Ubuntu打包的版本)运行它,我看到:

$ time ./magick.php > /dev/null
real    0m29.084s
user    0m42.289s
sys 0m4.716s
Run Code Online (Sandbox Code Playgroud)

或每张图片290毫秒。因此,在此测试中,php-vips的运行速度提高了10倍以上。这有点不公平:imagick可能比炮击合成更快。

这里还有另一个基准:

https://github.com/jcupitt/php-vips-bench

在这一点上,php-vips比imagick快4倍左右,所需内存减少8倍。

这是打包为Dockerfile的全部内容,您应该可以在任何地方运行:

https://github.com/jcupitt/docker-builds/tree/master/php-vips-ubuntu-16.04

  • 我无法表达我的感激之情。这些基准速度使我能够构建我的解决方案。这是一段漫长而艰难的研究旅程,我一遍又一遍地将头撞在墙上。谢谢。 (2认同)