简体   繁体   English

如何在 IMagick 中使用 SVG CSS 样式?

[英]How to use SVG CSS styles with IMagick?

It seems that the (PHP) Imagick does not accept styling with CSS even if the <style> is inside the SVG's <def> .即使<style>在 SVG 的<def>中,(PHP)Imagick 似乎也不接受 CSS 样式。 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;"然后将class="some-class"替换为stroke-dasharray="5 5" ,或者替换为style="stroke-dasharray: 5 5;" since Imagick seems to understand style attributes.因为 Imagick 似乎理解样式属性。

Seems to be a bug in librsvg.似乎是 librsvg 中的一个错误。 If I add type="text/css" to the <style> tag it works like a charm.如果我将type="text/css"添加到<style>标记,它就像一个魅力。

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.我将您的代码保存为 test.svg 文件,并使用带有 RSVG 的 Imagemagick 6.9.9.30 Q16 Mac OSX(librsvg @2.42.2_0),它工作正常。 I get a small red square:我得到一个小红方块:

convert test.svg test.png

在此处输入图像描述

What is your version of RSVG.您的 RSVG 版本是什么。

convert -list format

will tell you.会告诉你。

Perhaps you need to upgrade RSVG or your version of Imagemagick.也许您需要升级 RSVG 或您的 Imagemagick 版本。

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)我从 Imagick 安装了 inkscape 和 SVG 生成(实际上我使用的是干预图像,驱动程序设置为 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编辑:起初 SVG 生成根本不起作用,我按照这个线程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.然而,在那之后,SVG 生成工作,不幸的是,从 SVG 渲染的 PNG 是灰度的。 I thought the styles in the SVG were missing and this thread helped me: how to resize an SVG with Imagick/ImageMagick我认为 SVG 中的样式丢失了,这个线程帮助了我: 如何使用 Imagick/ImageMagick 调整 SVG 的大小

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. Imagemagick 似乎只考虑一个<style>标签,如果你有多个标签,它只会考虑最后一个,而忽略其他标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM