How to convert an SVG string into a jpg with Inkscape

前端 未结 2 782
梦毁少年i
梦毁少年i 2021-01-12 12:52

After having spent two days attempting to rasterize jpeg\'s from SVG strings using ImageMagick I have finally given up.

Although I managed to get the actual conversi

相关标签:
2条回答
  • 2021-01-12 13:11

    If you have an SVG string, and you're sending it from the browser to the server via AJAX, you'll need to write it to a temp file, so it can be referenced from the Inkscape command line. You can't render to JPEG using the Inkscape command line, but you can render to PNG easily, and if you really need a different format, of course you can convert using ImageMagick subsequently.

    You'll need something like:

    /path/to/inkscape \
        --without-gui \
        --export-png=/path/to/output.png \
        /tmp/file/input.svg
    

    If you are accepting full/partial SVG input from the user, bear in mind there are a good number of security issues you need to bear in mind. Happy to expand on this if required.

    0 讨论(0)
  • 2021-01-12 13:17

    You could pass your SVG string to inkscape using stdin, but the code is less portable.

    // Open Inkscape process
    $process = proc_open(
        '/path/to/inkscape -z -f /dev/fd/0 -e /path/to/output' 
        array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), 
        $pipes
    );
    
    // Write svg to stdin
    fwrite($pipes[0], $svg);
    
    // Close process
    foreach ($pipes as $pipe) fclose($pipe);
    proc_close($process);
    
    0 讨论(0)
提交回复
热议问题