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

编程入门 行业动态 更新时间:2024-10-27 16:29:02
本文介绍了readimageblob:将SVG转换为PNG时出现致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to use Image-Magick with PHP to convert SVG text into a PNG image. This SVG is a chart generated with NVD3 and I'd like to allow my users to download it as an image.

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

Basically, I'm sending the SVG data, encoded with JSON to the PHP handler which is supposed to output it as a PNG image.

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

But, this throws up the following error:

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脚本:

<?php /* Derived in part from: stackoverflow/a/4809562/937891 */ $svg=json_decode($_REQUEST["svgData"]); $im=new Imagick(); $im->readImageBlob($svg); $im->setImageFormat("png24"); header("Content-Type: image/png"); $thumbnail = $im->getImageBlob(); echo $thumbnail; ?>

HTML:

<form id="exportSpendTrendTrigger" method="POST" action="svg2png.php" target="_blank"> <input id="exportSpendTrendSvgData" type="hidden" name="svgData" /> <input type="submit" class="btn" value="Export" /> </form> <div id="spendtrend"> <svg></svg> </div>

jQuery:

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

由NVD3生成的

示例SVG : pastebin/Z3TvDK16

Example SVG, generated by NVD3: pastebin/Z3TvDK16

这是在装有PHP 5.3和Imagick的Ubuntu服务器上

This is on an Ubuntu server with PHP 5.3 and Imagick

推荐答案

svg文件文本结构需要为readImageBlob指定以解码文件. 在具有cvg文本的变量前加上<?xml version="1.0" encoding="UTF-8" standalone="no"?>.

svg file text structure needs to be specified for readImageBlob to decode the file. Prepend <?xml version="1.0" encoding="UTF-8" standalone="no"?> to your variable having svg text.

$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.$svg;

你很好.

更多推荐

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

本文发布于:2023-07-27 03:12:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   错误   readimageblob   SVG   PNG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!