5

on server side I have an generated SVG XML source code. This should be changed to an image in order to offer a PNG (or JPG) download from an SVG XML code. Searching the web for a long time, I only found this solution using ImageMagick. Convert SVG image to PNG with PHP But I don't have access to the ImageMagick library so I need a different way to convert SVG XML Code to a bitmap image.

Does anybody have an idea?

Brw: It's not an option to save that svg an execute an binary or script on operating system to convert.

Thank you.

Community
  • 1
  • 1
The Bndr
  • 13,204
  • 16
  • 68
  • 107
  • Do you mean that you create an SVG on the fly, and need to serve a PNG? Can you give more detail? Are you trying to solve a cross-browser problem? – andy256 Jul 06 '13 at 09:05
  • Inkscape can export SVG as PNG (save as doesn't seem to work yet), but this doesn't seem to meet the requirements as I understand them. – andy256 Jul 06 '13 at 09:30
  • Have you checked out [Batik](http://xmlgraphics.apache.org/batik/), [How to convert SVG into PNG on-the-fly](http://stackoverflow.com/questions/8167977/how-to-convert-svg-into-png-on-the-fly), and [Is there some Java library for converting .svg to .png or .jpg at code?](http://stackoverflow.com/questions/6610792/is-there-some-java-library-for-converting-svg-to-png-or-jpg-at-code)? They use java, but you should be able to hook it up. – andy256 Jul 07 '13 at 12:08
  • @andy256 No, it's not an cross-browser topic. At this web application the user is able to create an svg. So all graphic information insight the application are svg code. After creating the svg, it should be possible to download/export this svg as png. For this case, the web-application has to render the SVG to png. (jpg would also be an option). I have checked out your links. But I'm not sure, if that solution fits that problem. Because i don't know, how to execute java code insight the PHP interpreter (which generates th website) – The Bndr Jul 09 '13 at 09:32
  • Ok, we are getting outside of my expertize now. I see there is a [Php-Java bridge](http://php-java-bridge.sourceforge.net/doc/how_it_works.php) that can be used on the backend (along with other techniques), and another possibility is to build a Java applet that can be called (from Javascript) on the frontend ... – andy256 Jul 09 '13 at 10:33

1 Answers1

0

You need to use batik library. Download it, placed somewhere in your project. Then in php call the batik command using shell_exec() function. It will take few seconds and convert you svg to png.

Example:-

outputfile ='path where you want to lace png'
$tempSVG_filename = '/var/www' . $baseUrl . '/png/temp.svg';
$tempSVG_handle = fopen($tempSVG_filename, 'w+');
fwrite($tempSVG_handle, $YourSVG);
fclose($tempSVG_handle);
$mimetype = 'image/png';
$width = '6000';

$result = shell_exec('java  -jar /var/www/svgtopng/batik-1.7/batik-rasterizer.jar -m ' . $mimetype . ' -d ' . $outputfile . ' -w ' . $width . ' ' . $tempSVG_filename . ' 2>&1');
 unlink($tempSVG_filename);
RashFlash
  • 992
  • 2
  • 20
  • 40
  • Thank you for that answer, but it not fits my question, because: `It's not an option to save that svg an execute an binary or script on operating system to convert`. But exactly this is your suggestion. – The Bndr Jul 15 '13 at 11:09