加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – readimageblob:将SVG转换为PNG时的致命错误

发布时间:2020-12-13 16:33:55 所属栏目:PHP教程 来源:网络整理
导读:我试图使用 Image-Magick与 PHP将SVG文本转换为PNG图像.这个SVG是用 NVD3生成的图表,我想允许我的用户以图像的形式下载它. 基本上,我发送的SVG数据,用JSON编码到PHP处理程序,该处理程序应该将其作为PNG图像输出. 但是,这会抛出以下错误: Fatal error: Uncau
我试图使用 Image-Magick与 PHP将SVG文本转换为PNG图像.这个SVG是用 NVD3生成的图表,我想允许我的用户以图像的形式下载它.

基本上,我发送的SVG数据,用JSON编码到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脚本:

<?php
    /* Derived in part from: https://stackoverflow.com/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);

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

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

需要为readImageBlob指定svg文件文本结构来解码文件.
prepend<?xml version =“1.0”encoding =“UTF-8”standalone =“no”?>到你的变量有svg文本.
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.$svg;

你很好去

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读