ImageMagick 是覆盖图像的最快方法吗?我如何才能做得更快,或者是否有我不知道的更快的技术?
Is ImageMagick the fastest way to overlay images? How do I make faster or is there a faster tech I'm unaware of?
我正在构建图像处理 Nginx CDN/cache 服务器以在服装 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();
?>
这是一个示例结果:
我的问题是 ImageMagick 太慢了。除了 more CPU/Memory,还有什么技巧可以让它更快吗?是否有任何替代技术可以更快地叠加图像?
非常感谢任何帮助。
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();
}
使用来自您服务器的测试图像:
http://build9.hometownapparel.com/pics/
然后 运行ning 在我的台式机上(Ubuntu 17.10,一个快速的 i7 CPU)我看到了:
$ time ./overlay.php blank-tshirt.jpg GOSHEN.svg > /dev/null
real 0m2.488s
user 0m13.446s
sys 0m0.328s
所以每张图片大约需要 25 毫秒。我看到了这个结果(显然取自第一次迭代):
我尝试了您的 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);
}
运行 它针对 IM-6.9.7-4(为 Ubuntu 打包的版本)我看到:
$ time ./magick.php > /dev/null
real 0m29.084s
user 0m42.289s
sys 0m4.716s
或每张图片 290 毫秒。所以在这个测试中,php-vips 快了 10 倍多。这有点不公平:imagick 可能会比只用 composite 快一点。
这里还有另一个基准:
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
我正在构建图像处理 Nginx CDN/cache 服务器以在服装 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();
?>
这是一个示例结果:
我的问题是 ImageMagick 太慢了。除了 more CPU/Memory,还有什么技巧可以让它更快吗?是否有任何替代技术可以更快地叠加图像?
非常感谢任何帮助。
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();
}
使用来自您服务器的测试图像:
http://build9.hometownapparel.com/pics/
然后 运行ning 在我的台式机上(Ubuntu 17.10,一个快速的 i7 CPU)我看到了:
$ time ./overlay.php blank-tshirt.jpg GOSHEN.svg > /dev/null
real 0m2.488s
user 0m13.446s
sys 0m0.328s
所以每张图片大约需要 25 毫秒。我看到了这个结果(显然取自第一次迭代):
我尝试了您的 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);
}
运行 它针对 IM-6.9.7-4(为 Ubuntu 打包的版本)我看到:
$ time ./magick.php > /dev/null
real 0m29.084s
user 0m42.289s
sys 0m4.716s
或每张图片 290 毫秒。所以在这个测试中,php-vips 快了 10 倍多。这有点不公平:imagick 可能会比只用 composite 快一点。
这里还有另一个基准:
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