ImageMagick takes long time to convert SVG with text to JPEG

316 Views Asked by At

We are using ImageMagick (Version: ImageMagick 6.9.1-7 Q16 x86_64) and its PHP extension Imagick on our LAMP stack to convert SVGs to JPEGs and it is taking an abnormally long amount of time when the SVG contains any text (12-13 seconds / file).

When the same thing is run as a standalone PHP script from the command line (or directly using IM’s convert) it converts quickly in under 1 second regardless whether it has text or not.

Worth of mention also is that we do not have this problem with GraphicsMagick. (But it has some SVG bugs that are unresolved and prevent us from using it.)

Would anyone have an idea as to why the fonts take so long to process on the LAMP stack, or how to identify the root cause of the slowdown?

Sample 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>

and the code to convert:

$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

There are 1 best solutions below

0
On

That SVG converts just fine for me.

The way to investigate this is to run the PHP script through strace with a command like:

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

But with the appropriate path for PHP on your system.

There is a high chance there is some error condition that is making 'something' take far longer than it should do. The strace command above will allow you to see all of the system calls that are being made, and how long each of them take.

I suspect what you will see some system call take almost all of the total time for processing, before returning an error code. If you can't figure out what it's output means, please attach the output to your question, or pastebin, and I will attempt to interpret the runes.

Warning - strace is capable of capturing information that you might not want revealed (e.g. secret keys) so care should be made before posting its output anywhere.