Page 1 of 1

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

Posted: 2017-08-08T08:03:56-07:00
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.

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

Posted: 2017-08-08T08:46:38-07:00
by snibgo
ImageMagick uses a delegate to rasterize SVGs. Perhaps your two versions (command line, and via IMagick) use two different rasterizers.

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

Posted: 2017-08-08T08:54:32-07:00
by trevororr
How would I go about finding out what delegate each uses?

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

Posted: 2017-08-08T09:04:47-07:00
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)

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

Posted: 2017-08-08T09:09:09-07:00
by trevororr
When I run:
convert -list format

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

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

Posted: 2017-08-08T09:47:06-07:00
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.

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

Posted: 2017-08-08T10:19:02-07:00
by trevororr
I installed Inkscape and that seems to have fixed everything.

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

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

Posted: 2017-08-08T10:20:48-07:00
by fmw42
Glad that helped.