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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog