You are not logged in.
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
This is the result I'm getting: http://reformedtube.org/svg2png.php
Offline
Does anyone have any ideas? Am I using proc_open() correctly?
Offline
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
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
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
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