How to read SVG string in Imagick?

匿名 (未验证) 提交于 2019-12-03 09:58:14

问题:

I have a string containing markup for an svg element.

<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.

$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. Thanks

回答1:

You have to add <?xml version="1.0"?> in the begining of $svg variable.

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; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!