IMagick renders SVG groups offset. Help!

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
pfriedl
Posts: 12
Joined: 2015-04-22T10:00:38-07:00
Authentication code: 6789

IMagick renders SVG groups offset. Help!

Post by pfriedl »

Hi all. I've got an app that takes an SVG as input and converts to a PNG as output. I've run into a problem where if the SVG has grouped items using the <g> tag, that content gets rendered way off to the right and down on the SVG.

This only happens using IMagick. If I open the SVG in Illustrator or Inkscape, it looks fine. Here's what the SVG code looks like:

Code: Select all

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="2400px" height="3200px" xmlns:xlink="http://www.w3.org/1999/xlink">
	<g id="SvgjsG1048" transform="rotate(0 1200 1600) translate(30.927835051559143 41.23711340207863) scale(0.9742268041237008 0.9742268041237009) " x="30.927835051559143" y="41.23711340207863">
		<image id="SvgjsImage1049" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="aerosmith.png" width="2400" height="3200"></image>
	</g>
</svg>
and here's the code I'm using to pull in and render:

Code: Select all

// setup the SVG
$svg_front = new Imagick();
$svg_front->setBackgroundColor(new ImagickPixel('transparent'));
$svg_front->readImage($json->imageFront); // pull in the SVG image

// convert the SVG to PNG @ 200 dpi - yes, it works!
$svg_front->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$svg_front->setImageResolution(200,200);
$svg_front->setImageFormat('png');

// render the SVG
echo $svg_front;
Nothing out of the ordinary here. Now, if I remove the <g> tag and leave the image in there, then the SVG renders with no issue. However, I can't control whether the app sends grouped items or not, and IMagick needs to render it no matter what.

Here's the image when it gets rendered to PNG with a <g> tag in the SVG: http://brainboxinteractive.com/bad-svg.png

And here's what is should look like: http://brainboxinteractive.com/what-it- ... k-like.png

I've got inkscape installed on the server as well, so I'm not sure where to start debugging and fixing this issue. Any help would be appreciated!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: IMagick renders SVG groups offset. Help!

Post by snibgo »

I know nothing about IMagick.

With ImageMagick, put Inkscape in the path, and IM will use Inkscape to render SVG.
snibgo's IM pages: im.snibgo.com
pfriedl
Posts: 12
Joined: 2015-04-22T10:00:38-07:00
Authentication code: 6789

Re: IMagick renders SVG groups offset. Help!

Post by pfriedl »

Yes, I've got InkScape on the server, but I'm using the IMagick php extension. That will use Inkscape, but I'm still getting the results mentioned above.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: IMagick renders SVG groups offset. Help!

Post by fmw42 »

Does it also happen if you put the command line equivalent into a PHP exec() command?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: IMagick renders SVG groups offset. Help!

Post by fmw42 »

$svg_front->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
PNG does not allow PixelsPerInch, though Imagemagick should convert automatically. I am not sure how Imagick handles that.
pfriedl
Posts: 12
Joined: 2015-04-22T10:00:38-07:00
Authentication code: 6789

Re: IMagick renders SVG groups offset. Help!

Post by pfriedl »

Not sure what the exec command would be for the same operation. But I have to use IMagick and have the feedback as this is all happening in a script and I'm using the result to create composite images. exec wouldn't work in that case.

When I use the pixels per inch, it does convert the SVG (which is 96 dpi for some reason) to the 2400x3200 PNG at 200 dpi, so that seems to be working.

Still, if I comment that entire section out and just use

Code: Select all

$svg_front->setImageFormat('png');
by itself, the result is still everything shifted to down and right. That's the issue I'm having problems with - any idea how to fix that?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: IMagick renders SVG groups offset. Help!

Post by fmw42 »

You may need to reset the virtual canvas after reading the input file. I do not know what the equivalent is of +repage or -repage +0+0 in Imagick.
pfriedl
Posts: 12
Joined: 2015-04-22T10:00:38-07:00
Authentication code: 6789

Re: IMagick renders SVG groups offset. Help!

Post by pfriedl »

Nope, IMagick->setImagePage doesn't seem to do the trick. Any ideas why a group tag in an SVG is offsetting the graphics?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: IMagick renders SVG groups offset. Help!

Post by fmw42 »

Sorry, I am not an SVG expert.
pfriedl
Posts: 12
Joined: 2015-04-22T10:00:38-07:00
Authentication code: 6789

Re: IMagick renders SVG groups offset. Help!

Post by pfriedl »

^BUMP^ is there anyone that can figure this out? At this point, I'm willing to pay consulting fees.
Post Reply