You are not logged in.

#1 2010-03-16 03:40:38

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

As the title suggests, I'm trying to use PHP+Imagemagick to convert svg to a raster JPG on the fly. Eventually the script will be reading in SVG from a file but at the moment hard-coding the SVG does not seem to be working... Any ideas?

<?php
    header("Content-type: image/jpeg");
    $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"));
    $convert = proc_open("/usr/bin/convert -quality 100 svg:- jpg:-", $descriptorspec, $pipes);
    fwrite($pipes[0], "<?xml version=\"1.0\" standalone=\"no\"?><svg width=\"100%\" height=\"100%\" version=\"1.1\"><circle cx=\"100\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"2\" fill=\"red\"/></svg>");
    fclose($pipes[0]);
    while(!feof($pipes[1]))
        $jpg .= fread($pipes[1], 1024);
    fclose($pipes[1]);
    proc_close($convert);
    return(stripslashes($jpg));
?>

Last edited by tony5429 (2010-03-18 04:10:31)

Offline

#2 2010-03-16 15:25:32

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

This is the result I'm getting: http://reformedtube.org/svg2png.php

Offline

#3 2010-03-18 01:52:26

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

Does anyone have any ideas? Am I using proc_open() correctly?

Offline

#4 2010-03-18 03:01:01

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

Um, where are you actually writing out the output?  Shouldn't there be an "echo $jpg" or similar?  "return" at global scope just terminates the script.

EDIT: Also, stripslashes is not going to work on binary data like jpeg files.

Last edited by tavianator (2010-03-18 03:02:13)

Offline

#5 2010-03-18 03:46:52

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

Thank you tavianator for your tip. It makes sense but unfortunately I am still getting no output...

<?php
    header("Content-type: image/jpeg");
    $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"));
    $convert = proc_open("/usr/bin/convert -quality 100 svg:- jpg:-", $descriptorspec, $pipes);
    fwrite($pipes[0], "<?xml version=\"1.0\" standalone=\"no\"?><svg width=\"100%\" height=\"100%\" version=\"1.1\"><circle cx=\"100\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"2\" fill=\"red\"/></svg>");
    fclose($pipes[0]);
    while(!feof($pipes[1]))
        $jpg .= fread($pipes[1], 1024);
    fclose($pipes[1]);
    proc_close($convert);
    echo $jpg;
?>

Direct link: http://reformedtube.org/svg2png.php

Offline

#6 2010-03-18 03:52:52

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

Well I just tried running that with php -a (the interactive php shell, very helpful btw), and got this error:

convert: no decode delegate for this image format `-' @ error/svg.c/ReadSVGImage/2815.
convert: missing an image filename `jpg:-' @ error/convert.c/ConvertImageCommand/2919.

I'm no imagemagick expert, so I'm not sure of the way to fix it.

Offline

#7 2010-03-18 04:10:16

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] Using PHP+Imagemagick to Rasterise SVG on the Fly

Thanks - that was the problem - the SVG data I was giving it had an error. I had to set the width and height in pixels, not percent. It works now. Thank you, tavianator!

Offline

Board footer

Powered by FluxBB