PHP SVG read on OSX Mavericks (MAMP 3) using Imagick

。_饼干妹妹 提交于 2019-12-25 02:25:26

问题


I have nearly the same problem like here: ImagickException with message Postscript delegate failed on MAMP 3.0.5

I would like to read an SVG file (5 set Venn Diagram), which I created with php and I would like to write it out to a png/jpeg or whatever file... nothing work. It breaks on the 3rd line:

$im = new Imagick();
$svg = $venn_diagram;
$im->readImageBlob($svg);
$im->setImageFormat("jpeg");
$im->adaptiveResizeImage(720, 445); 
$im->writeImage($folder . 'output_venn_diagram.png');
$im->clear();
$im->destroy();

With this:

Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' @ error/blob.c/BlobToImage/359' in /myphp.php:500 Stack trace: #0 /myphp.php(500): Imagick->readimageblob('<svg version='1...') #1 {main} thrown in /myphp.php on line 500

It breaks also with this very simplified SVG also:

<svg version='1.0' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='746' height='742' viewBox='-362 -388 746 742' encoding='UTF-8' standalone='no'>
    <defs>
        <ellipse id='ellipse' cx='36' cy='-56' rx='160' ry='320' />
        <g id='ellipses'>
            <use xlink:href='#ellipse' fill='#0000ff' />
            <use xlink:href='#ellipse' fill='#0099ff' transform='rotate(72)' />
        </g>
    </defs>
</svg>

I don't really know what to do:

  • MAMP / php running.
  • I have SVG specification in the beginning of my file, I checked.
  • I installed, uninstalled, reinstalled (with brew) imagick several times.
  • I restarted MAMP also.

Do somebody know what to do?

Thanks for help!

  • OS X Mavericks 10.9.3
  • MAMP 3.05
  • php 5.5.10
  • imagemagick 6.8.9-1

回答1:


Your SVG isn't valid xml and so isn't valid SVG

Add this to the start of the SVG:

<?xml version="1.0"?>

And it should work. The complete code that works for me is:

  $svg = <<< END
<?xml version="1.0"?>
<svg version='1.0' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='746' height='742' viewBox='-362 -388 746 742' encoding='UTF-8' standalone='no'>
    <defs>
        <ellipse id='ellipse' cx='36' cy='-56' rx='160' ry='320' />
        <g id='ellipses'>
            <use xlink:href='#ellipse' fill='#0000ff' />
            <use xlink:href='#ellipse' fill='#0099ff' transform='rotate(72)' />
        </g>
    </defs>
</svg>

END;


        $image = new \Imagick();

        $image->readImageBlob($svg);
        $image->setImageFormat("jpg");
        header("Content-Type: image/jpg");
        echo $image;

Produces this image:



来源:https://stackoverflow.com/questions/24387379/php-svg-read-on-osx-mavericks-mamp-3-using-imagick

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