1

I'm trying to convert SVG to PNG files via PHP. I've resigned myself to the fact that the only reasonable way to do this is via ImageMagick.

I am doing development work on a xampp setup in Windows 7, and production will be in a linux environment. The following PHP code is successful in a file-conversion:

$svg_file_name = "image.svg";
$png_file_name = "image.png";
system("convert -background none $svg_file_name $png_file_name");

However, the production server has access to the "system()" method disabled for security reasons. The following code (slightly modified but originally suggested here) fails on line four:

<?
$image = new Imagick();
$contents = file_get_contents('image.svg');
$image->readImageBlob($contents);
$image->setImageFormat("png24");
$image->resizeImage(400, 225, imagick::FILTER_LANCZOS, 1); 
$image->writeImage('image.png');

with the error:

Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' @ error/blob.c/BlobToImage/358' in C:\Web\xampp\htdocs\sitename.com\editor\image.php:4 Stack trace: #0 C:\Web\xampp\htdocs\sitename.com\editor\image.php(4): Imagick->readimageblob('<svg width="640...') #1 {main} thrown in C:\Web\xampp\htdocs\sitename.com\editor\image.php on line 4

The preceding code works when a .jpg file is used as input.

The contents of the SVG file are as follows:

<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
 <g>
  <circle id="svg_1" r="72.73239" cy="89" cx="210" stroke-width="5" stroke="#000000" fill="#FF0000"/>
  <circle id="svg_2" r="71.06335" cy="155" cx="383" stroke-width="5" stroke="#000000" fill="#FF0000"/>
 </g>
</svg>

I've read through a few dozen discussions on similar topics, but none of them are running in a windows environment. I'm not sure what to make of the "no decode delegate" message, because my phpinfo() lists SVG as an "ImageMagick Supported format."

Community
  • 1
  • 1
Brad Clark
  • 11
  • 1
  • I found the answer [here](http://stackoverflow.com/questions/13125352/readimageblob-fatal-error-when-converting-svg-into-png). Not sure what I searched for differently this time... – Brad Clark Jul 17 '13 at 23:34

0 Answers0