PHPでImage-Magickを使用して、SVGテキストをPNG画像に変換しようとしています。このSVGはNVD3で生成されたチャートであり、ユーザーが画像としてダウンロードできるようにしたいと思います。
基本的に、JSONでエンコードされたSVGデータを、PNG画像として出力することになっているPHPハンドラーに送信します。
ただし、これにより次のエラーが発生します。
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: http://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);
NVD3によって生成されたSVGの例:http: //pastebin.com/Z3TvDK16
これは、PHP5.3とImagickを搭載したUbuntuサーバー上にあります