Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

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
trevororr
Posts: 12
Joined: 2014-02-18T12:36:54-07:00
Authentication code: 6789

Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by trevororr » 2017-08-08T08:03:56-07:00

I am having an issue with converting a SVG image to PNG with PHP using IMagick and preserving opacity on some SVG elements. I had a windows server and used Imagick from command line to convert SVG to PNG and everything worked great. I am now switching to PHP website on a unix server and I am using PHP IMagick.

It converts the file and preserves background transparency fine. I have a bunch of elements in the SVG that get turned on and off and then converted to a PNG. The PHP IMagick does not seem to handle opacity on an element in SVG though. If I view the SVG in my browser it is correct, just the converted file is incorrect.

I have tried the following on elements and none of them seem to hide the elements.

style="opacity: 0;"
opacity="0"
visibility="hidden"

Is this a known issue with Imagick or am I missing something here.

snibgo
Posts: 9485
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by snibgo » 2017-08-08T08:46:38-07:00

ImageMagick uses a delegate to rasterize SVGs. Perhaps your two versions (command line, and via IMagick) use two different rasterizers.
snibgo's IM pages: im.snibgo.com

trevororr
Posts: 12
Joined: 2014-02-18T12:36:54-07:00
Authentication code: 6789

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by trevororr » 2017-08-08T08:54:32-07:00

How would I go about finding out what delegate each uses?

User avatar
fmw42
Posts: 22234
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by fmw42 » 2017-08-08T09:04:47-07:00

I do not know about Imagick. But in Imagemagick, you can do the command

Code: Select all

convert -list format
and look at the line for SVG. It should say (RSVG, XML/MSVG or Inkscape)

trevororr
Posts: 12
Joined: 2014-02-18T12:36:54-07:00
Authentication code: 6789

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by trevororr » 2017-08-08T09:09:09-07:00

When I run:
convert -list format

I get:
SVG SVG rw+ Scalable Vector Graphics (XML 2.9.4)

User avatar
fmw42
Posts: 22234
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by fmw42 » 2017-08-08T09:47:06-07:00

That means it is using the Imagemagick internal SVG renderer, which is the worst of the 3. The best is to install Inkscape on your system. Imagemaick will use that if it can find it on your system. Second is to install the RSVG delegate library and recompile Imagemagick with it added.

My guess is neither is installed such that Imagick can access it. So it defaults to the Imagemagick MSVG/XML.

trevororr
Posts: 12
Joined: 2014-02-18T12:36:54-07:00
Authentication code: 6789

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by trevororr » 2017-08-08T10:19:02-07:00

I installed Inkscape and that seems to have fixed everything.

Thank you so much!!!! I have been struggling with this for hours.

User avatar
fmw42
Posts: 22234
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Convert SVG image to PNG with PHP Imagick and preserve opacity on elements

Post by fmw42 » 2017-08-08T10:20:48-07:00

Glad that helped.

Post Reply