readimageblob:将SVG转换为PNG时出现致命错误

我正在尝试使用Image-Magick和PHP将SVG文本转换为PNG图像。 此SVG是使用NVD3生成的图表,我希望允许我的用户将其下载为图像。

基本上,我将用JSON编码的SVG数据发送到PHP处理程序,该处理程序应该将其输出为PNG图像。

但是,这会引发以下错误:

Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' @ blob.c/BlobToImage/347' in svg2png.php:4 Stack trace: #0 svg2png.php(4): Imagick->readimageblob(' 

用于转换图像的PHP脚本

 readImageBlob($svg); $im->setImageFormat("png24"); header("Content-Type: image/png"); $thumbnail = $im->getImageBlob(); echo $thumbnail; ?> 

HTML:

 

jQuery的:

 exportSpendTrend = function (e) { //Show the user the PNG-image version for download $("#exportSpendTrendSvgData").val( JSON.stringify($("#spendtrend").html().trim()) ); } $("#exportSpendTrendTrigger").on("submit", exportSpendTrend); 

示例SVG ,由NVD3生成: http ://pastebin.com/Z3TvDK16

这是在使用PHP 5.3和Imagick的Ubuntu服务器上

需要为readImageBlob指定svg文件文本结构来解码文件。 将到具有svg文本的变量中。

 $svg = ''.$svg; 

你很高兴。

记得那个

  

必须是包含SVG文本的变量的第一行。 知道这可能会让你有些头疼。

Interesting Posts