doujing2497 2015-05-12 10:51
浏览 91
已采纳

使用Imagick和PHP将SVG作为JPEG提供服务

I have an SVG string that I want to serve as JPEG using PHP. For this (and a lot more things) I am using Imagick. Below is the code from PHP:

$svg = '<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="28.125px" height="28.125px" viewBox="0 0 28.125 28.125" style="enable-background:new 0 0 28.125 28.125;" xml:space="preserve" background="transparent"><g><g><path d="M14.063,0L1.659,6.617v0.464c0,13.979,11.906,20.767,12.026,20.834l0.378,0.21l0.377-0.21   c0.12-0.067,12.026-6.854,12.026-20.834V6.617L14.063,0z M14.063,26.329C12.13,25.096,3.445,18.92,3.214,7.544l10.849-5.787 l10.849,5.786C24.681,18.919,15.996,25.094,14.063,26.329z"/><path d="M4.004,8.001c0.416,10.17,7.652,15.812,10.059,17.412c2.407-1.601,9.643-7.241,10.059-17.411L14.063,2.636L4.004,8.001z M14,21.191c-1.067,0-1.778-0.771-1.778-1.799c0-1.047,0.731-1.799,1.778-1.799c1.048,0,1.739,0.752,1.759,1.799 C15.759,20.421,15.067,21.191,14,21.191z M15.127,16.605h-2.253l-0.456-8.954h3.144L15.127,16.605z"/></g></g></svg>';
$image = new Imagick();
$image->readImageBlob($svg);
$image->setImageFormat("jpg");
header("Content-Type: image/jpg");
echo $image;

The problem here is that I get the following error when executed:

Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' @ error/blob.c/BlobToImage/358' in C:\xampp\htdocs\univar\build\makesprite.php:10 Stack trace: #0 C:\xampp\htdocs\univar\build\makesprite.php(10): Imagick->readimageblob('<?xml version="...') #1 {main} thrown in C:\xampp\htdocs\univar\build\makesprite.php on line 10

I am clueless as to what is happening. I tried reading a SVG file as well but to no success. Can someone please help?

  • 写回答

1条回答 默认 最新

  • duan205571 2015-05-21 10:11
    关注

    I finally found a combination that works. After a bit of struggle, I was finally able to get the below combination to work on my machine.

    OS: Windows 7 64 Bit
    PHP: 5.5.6
    XAMPP: 1.8.3
    Compiler: MSVC11
    Architecture: x86
    ImageMagick: ImageMagick-6.7.7-5-Q16-windows-dll (available here)
    Imagick DLL: PHP5.5.x version from http://www.peewit.fr/imagick/

    Note: After I tried this, Apache started without any error messages. I do have ImageMagick installed in a directory that has no spaces in the name (as some have suggested that might be a problem with some installs). In my case, I installed it to C:\imagemagick.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?