简体   繁体   English

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

[英]How to read SVG string in Imagick?

I have a string containing markup for an svg element. 我有一个包含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>

How can I read this string in Imagick and display it. 如何在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;

How can I read this svg string so that I can proceed with doing other stuffs. 如何读取这个svg字符串,以便我可以继续做其他的事情。 Thanks 谢谢

You have to add <?xml version="1.0"?> in the begining of $svg variable. 你必须在$ svg变量的开头添加<?xml version="1.0"?>

This code works for me: 这段代码适合我:

$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;

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

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