如何在 Imagick 中读取 SVG 字符串?

2024-05-06

我有一个包含 svg 元素标记的字符串。

<svg id="someId" width="300" height="300">
    <polygon id="another_id" fill="green" stroke="black" stroke-width="5" points="200,100 131,5 19,41 19,159 131,195 "></polygon>
</svg>

我怎样才能在Imagick中读取这个字符串并显示它。

$svg = '<svg id="someId" width="300" height="300"><polygon id="another_id" fill="green" stroke="black" stroke-width="5" points="200,100 131,5 19,41 19,159 131,195 "></polygon>
</svg>';

$image = new Imagick();
// This is not working. 
$image->readImageBlob($svg);
$image->setImageFormat("png");
header("Content-Type: image/png");
echo $image;

我怎样才能读取这个 svg 字符串,以便我可以继续做其他事情。谢谢


你必须添加<?xml version="1.0"?>在 $svg 变量的开头。

这段代码对我有用:

$svg = '<?xml version="1.0"?><svg id="someId" width="300" height="300"><polygon id="another_id" fill="green" stroke="black" stroke-width="5" points="200,100 131,5 19,41 19,159 131,195 "></polygon></svg>';
$image = new Imagick();
$image->readImageBlob($svg);
$image->setImageFormat("png");
header("Content-Type: image/png");
echo $image;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Imagick 中读取 SVG 字符串? 的相关文章

随机推荐