1

It seems that the (PHP) Imagick does not accept styling with CSS even if the <style> is inside the SVG's <def>. Is that correct? I couldn't find a reference for this.

If so, what would be a way to still get the desired results? Maybe replace all the classes with their definitions? So, for example, if there is

.some-class {
  stroke-dasharray: 5 5;
}

then replace class="some-class" by stroke-dasharray="5 5", or, alternatively, by style="stroke-dasharray: 5 5;" since Imagick seems to understand style attributes.

Daniel
  • 3,383
  • 4
  • 30
  • 61
  • I am not sure I understand your question, but Imagemagick can use either RSVG delegate or Inkscape to process an SVG file to render it to some raster format or view it. RSVG must be installed and then Imagemagick compiled to use RSVG. If Inkscape is on your system, the Imagemagick can use it without recompiling. – fmw42 Mar 27 '18 at 19:24
  • @fmw42 Good to know. I wasn't aware. However, I cannot influence Imagick on the server I am using. And it does not respect any css styles. – Daniel Mar 27 '18 at 19:29
  • Does your version of Imagemagick include RSVG as a delegate? You can see if `convert -version` lists RSVG. Imagick should use it if it is installed with Imagemagick. But what is your Imagick command that you are trying to invoke? Again, perhaps I misunderstand your question. – fmw42 Mar 27 '18 at 19:31
  • Do you have Inkscape on your system? If not, try adding that and see if you can convert your SVG file to a raster format? – fmw42 Mar 27 '18 at 19:48
  • @fmw42 I am using php's Imagick: https://secure.php.net/manual/en/class.imagick.php – Daniel Mar 27 '18 at 20:06
  • @fmw42 I am not sure how to do `convert -version`. But `::getVersion` gives me `Array ( [versionNumber] => 1673 [versionString] => ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org ) ` if that helps. – Daniel Mar 27 '18 at 20:26
  • That does not tell us the delegates installed with Imagemagick. You can find it via PHP exec as follows: `";} ?>` – fmw42 Mar 27 '18 at 20:48
  • @fmw42 Unfortunately, that produces only `exec() has been disabled for security reasons` – Daniel Mar 27 '18 at 20:52
  • try replacing exec() with system() or shell_exec() – fmw42 Mar 27 '18 at 20:53
  • @fmw42 That gives the same result. I have Inkscape on my local computer and it accepts classes and class definitions in SVG. So I guess on the server it does not render with Inkscape (or at least not a version that supports it). – Daniel Mar 28 '18 at 07:31
  • @fmw42 I could run the exec() command on a local server that has Imagick installed and faces the same problem. But that just gave me a blank output from php. – Daniel Mar 28 '18 at 07:36
  • Can you post your SVG file? I could then test it. – fmw42 Mar 28 '18 at 16:55
  • @fmw43 Here is a very minimal one: ``. By the way, have you seen my answer below? It might be a bug in RSVG. – Daniel Mar 28 '18 at 19:08

4 Answers4

2

Seems to be a bug in librsvg. If I add type="text/css" to the <style> tag it works like a charm.

Daniel
  • 3,383
  • 4
  • 30
  • 61
  • This is an old thread, but for anyone coming later, this fixed it for me!!! ImageMagick 7.0.7-11 Q16 x64 2017-11-23 http://www.imagemagick.org v1799 – Terry Wilkinson Jun 26 '23 at 22:09
1

I saved your code to a file as test.svg and used Imagemagick 6.9.9.30 Q16 Mac OSX with RSVG (librsvg @2.42.2_0) and it works fine. I get a small red square:

convert test.svg test.png

enter image description here

What is your version of RSVG.

convert -list format

will tell you.

Perhaps you need to upgrade RSVG or your version of Imagemagick.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks. That might be true. As I said I cannot run the exec command (or any of the other) to check. I also don't have access to the server, so I'll have to live with the old RSVG version, if that is the problem. – Daniel Mar 28 '18 at 19:38
  • 1
    Ask your hosting provider if they will upgrade RSVG for use with Imagemagick. I ran the same command using IM 6.8.9.9 with my current version of librsvg and it works fine. – fmw42 Mar 28 '18 at 19:42
  • Good idea. I'll try. – Daniel Mar 28 '18 at 19:48
1

I had the same issue recently, was working on my localhost but not on the server, due to a missing package. I installed inkscape and the SVG generation from Imagick (actually I'm using Intervention Image with the driver set to imagick)

apt-get install inkscape

EDIT: at first the SVG generation was not working at all and I followed this thread Imagick SVG to JPG error no decode delegate

However,after that, the SVG generation was working, unfortunately the rendered PNG from a SVG was in greyscale. I thought the styles in the SVG were missing and this thread helped me: how to resize an SVG with Imagick/ImageMagick

Aurel
  • 405
  • 5
  • 8
0

A small caveat for those that may be at their wits end. Imagemagick seems to only consider one <style> tag, if you have more than one, it will only consider the last, and disregard the others.

dearsina
  • 4,774
  • 2
  • 28
  • 34