Question

Please help. I was using php Imagick library to successfully convert svg (including embedded png image in <image/> tag) to png image till recently. I guess after a few updates on ubuntu the embedded png image stopped appearing in the converted image from svg.

$svg =  json_decode($_POST['svgdata']);
$svg = '<?xml version="1.1" encoding="UTF-8" standalone="no"?>'.$svg;
$im = new Imagick();
$success = $im->readImageBlob($svg);
$im->setImageFormat("png24");
$im->writeImage('png_img/var.png');

If I echo $svg it gives me a correct image. output of echo $svg;

whereas the saved image isenter image description here.

For reference:

$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="overflow: hidden; position: relative; -moz-user-select: text;" xmlns="http://www.w3.org/2000/svg" width="794" version="1.1" height="122">
<image class="barcode" style="-moz-user-select: text;" xlink:href="images/52aab69d.png" preserveAspectRatio="none" height="40" width="266" y="30" x="380"></image>
<rect class="ref" style="-moz-user-select: text;" stroke="#000" fill="#000000" ry="0" rx="0" r="0" height="18" width="18" y="15" x="10"></rect></svg>';
Was it helpful?

Solution

I found the solution. I had to do it in a totally different way. I took help from -> converting image to base64.

Now that the image has been embedded in the SVG the php code successfully converts the svg to PNG image. Just replaced the image path with base64 string.

OTHER TIPS

The xlink:href should be a full qualified domain name. The relative path "images/52aab69d.png" will resolve through your web browser, but may not within the server path.

<image class="barcode" 
       style="-moz-user-select: text;" 
       xlink:href="http://localhost/images/52aab69d.png" 
       preserveAspectRatio="none" 
       height="40" width="266"
       y="30" x="380"></image>

The interesting workaround found by Puneet doesn't really solve the problem mentioned:

  1. let's have an svg A embedded into another svg file B.svg
  2. convert B into another format - let's say png- using Imagick
  3. A is not processed and not shown in the converted file B.png

This is a problem, expecially if your purpose is slightly different: for example if you need to convert your SVG into a PostScript file in order to send it to a printer, the solution mentioned by Puneet doesn't work.

Lets analyze the problem:

Imagick doesn't convert just the embedded svg. I tried using Gmagick, another library used by big companies. No way.

I tried using the "convert" command line tool (I'm in a Debian 8 environment).

convert withNestedSvgTag.svg withNestedSvgTag.ps

No way.

It seems that the parser that parses the SVG file ignores completely the nested <svg> tags, accepting only the root <svg> tag.

I have another server, (Debian 7 and Imagick 3.1.0RC1), and it works like a sharm.

The other server seems rendering recursively each svg nested tag.

This means that the problem isn't the SVG file or the PHP code (consider that the Imagick class library simply wraps Imagemagick).

So, in conclusion, the real solutions are:

1.1 - Amend the Imagemagick code (I'll write to the community of developers reporting the problem in the bug section)

1.2 - Substituting the tag with another one, like the tag.

The 1.2 is another workaround, I know, but it seems working, obviously you have to set up properly the size of the elements in the group tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top