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 »

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: 12159
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 »

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 »

How would I go about finding out what delegate each uses?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

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 »

When I run:
convert -list format

I get:
SVG SVG rw+ Scalable Vector Graphics (XML 2.9.4)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

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 »

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: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Glad that helped.
Post Reply