【问题标题】:Converting SVG to PNG with ImageMagic ignores fill pattern url使用 ImageMagic 将 SVG 转换为 PNG 会忽略填充模式 url
【发布时间】:2016-06-13 21:40:33
【问题描述】:

我正在使用 PHP 的 Imagick 类将 SVG 转换为 PNG。

转换成功,但似乎忽略了任何填充模式。这是我的代码:

<?php

$svg = <<<SVG
<?xml version="1.0"?>
<svg>
    <defs>
        <pattern id="myPattern" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
            <rect width="20" height="20" fill="#ffffff" /> 
            <circle cx="10" cy="10" r="5" stroke="#0000bb" fill="#dd8d8d" /> 
        </pattern>
    </defs>
    <text y="100%"
        stroke="#ff0000"
        fill="url('#myPattern')"
        font-size="40pt">
        Hello <tspan style="font-style: italic">SVG</tspan> world!
    </text>
</svg>
SVG;

try{

    $image = new Imagick();
    $image->setBackgroundColor(new ImagickPixel('transparent'));
    $image->readImageBlob( $svg );
    $image->setImageFormat('png32');
    $image->setImageCompressionQuality(100);

    file_put_contents('text-fill-example.png', $image);
}
catch(ImagickException $e){
    error_log($e);
}

在上面的例子中,字母应该包含一个点状图案。如果您在 Inkscape 中将上述 svg 代码作为文件打开,则该模式会正确显示。此外,如果您将 svg 代码嵌入到 html 中,它会在现代浏览器中正确显示该模式。

如果我尝试从命令行转换,该模式也会被忽略:

$ convert -verbose text-fill-example.svg text-fill-example.png 
text-fill-example.svg SVG 358x36 358x36+0+0 16-bit DirectClass 433B 0.020u 0:00.030
text-fill-example.svg=>text-fill-example.png SVG 358x36 358x36+0+0 16-bit PseudoClass 256c 8.19KB 0.010u 0:00.009

$ convert --version
Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP    

有人知道吗?

【问题讨论】:

  • 我的问题可能与stackoverflow.com/questions/35632706/… 这个问题有关。也许与不关注url('') 锚点有关?
  • 更新:我目前正在阅读 SVG IRI:https://www.w3.org/TR/SVG/linking.html。我试图也包括 xlink 命名空间。 xmlns:xlink="http://www.w3.org/1999/xlink" 尝试不同的语法变化...

标签: php svg imagick imagemagick-convert


【解决方案1】:

原来我的 SVG 代码并不完全有效。我使用W3C validator 最终得到了一些 100% 有效的标记,现在它可以工作了:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
    <defs>
        <pattern id="myPattern" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
            <rect width="20" height="20" fill="#ffffff" /> 
            <circle cx="10" cy="10" r="5" stroke="#0000bb" fill="#dd8d8d" /> 
        </pattern>
    </defs>
    <text y="100%"
        stroke="#ff0000"
        fill="url(#myPattern)"
        font-size="40pt">
        Hello <tspan style="font-style: italic">SVG</tspan> world!
    </text>
</svg>

【讨论】:

  • 对于其他遇到这篇文章的人,我发现原始 SVG 通过命令行使用 convert -verbose -background none MSVG:so.svg -resize 640x640 out.png 给出:must specify image size.. 我需要的最小更改是将 width="620mm" height="640mm" 添加到 @ 987654326@ 元素,并从fill 中删除单引号字符,即fill="url(#myPattern)"。要获得非空白输出图像,我还需要在 &lt;svg&gt; 元素上使用 viewBox="0 0 200 100"。 (编辑:这些尺寸来自我的项目,不适合这个示例)
猜你喜欢
  • 1970-01-01
  • 2021-08-19
  • 2021-05-27
  • 2015-12-12
  • 1970-01-01
  • 2013-12-29
  • 2015-10-04
  • 2015-04-06
  • 1970-01-01
相关资源
最近更新 更多