ImageMagick需要很长时间才能将带有文本的SVG转换为JPEG

时间:2015-07-17 03:54:19

标签: imagemagick lamp imagick graphicsmagick gmagick

我们在LAMP堆栈上使用ImageMagick(版本:ImageMagick 6.9.1-7 Q16 x86_64)及其PHP扩展名Imagick将SVG转换为JPEG,当SVG包含任何文本时,它需要花费非常长的时间( 12-13秒/档)。

当从命令行运行同一个独立的PHP脚本(或直接使用IM的转换)时,无论是否有文本,它都会在1秒内快速转换。

值得一提的是我们对GraphicsMagick没有这个问题。 (但它有一些未解决的SVG错误,阻止我们使用它。)

有没有人知道为什么字体在LAMP堆栈上处理这么长时间,或者如何识别减速的根本原因?

示例SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg"     xmlns:xlink="http://www.w3.org/1999/xlink" width="344" height="243"  viewBox="0 0 737 521">
<g class="main">
    <title>Main</title>
    <image xmlns:xlink="http://www.w3.org/1999/xlink" id="svg_12"  height="218.4417" width="291.2556" y="32.2537" x="-10.893" xlink:href="/tmp/767756670842438737_7032fbfb3c364e6da226254687eb1edb.jpg" style="pointer-events:inherit">29.75235 32.253654 209.964875 218.441697</image>
    <g font-size="normal" font-family="Allerta" class="textarea" id="svg_13" style="pointer-events:inherit">
        <rect opacity="0" fill-opacity="0" stroke-opacity="0" stroke-width="2" fill="#000" id="svg_10" height="32" width="150.4384" y="293.06824" x="550.14683" style="pointer-events:inherit"/>
        <text text-anchor="start" xml:space="preserve" fill="#000" font-size="21" y="293" x="550" id="svg_68" style="pointer-events:inherit">
            <tspan dy="14" x="550" xml:space="preserve" id="svg_69" style="pointer-events:inherit">
        hello </tspan>
            <tspan dy="21" x="550" xml:space="preserve" id="svg_70" style="pointer-events:inherit">gc </tspan>
        </text>
    </g>
</g>
</svg>

和要转换的代码:

$im = new Imagick(); 
$svg = file_get_contents($svgFile); 
$svg = str_replace(array("\n", "\r", "\t"), '', $svg); 
$im->readImageBlob($svg); 
$im->setImageFormat("jpeg"); 
$im->writeImage($jpgFile); 
$im->clear(); 

1 个答案:

答案 0 :(得分:0)

SVG转换为我很好。

调查此问题的方法是使用以下命令通过strace运行PHP脚本:

strace -f -F /usr/local/bin/php testScript.php

但是在您的系统上使用适当的PHP路径。

很可能会出现一些错误情况。花费的时间远远超过应有的时间。上面的strace命令将允许您查看正在进行的所有系统调用,以及每个调用所需的时间。

我怀疑在返回错误代码之前,您会看到一些系统调用占用了几乎所有的处理总时间。如果你无法弄清楚它的输出意味着什么,请将输出附加到你的问题或者pastebin,我会尝试解释符文。

警告 - strace能够捕获您可能不想泄露的信息(例如密钥),因此在将其输出发布到任何地方之前应该小心。