简体   繁体   English

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

[英]Serving SVG as JPEG using Imagick and PHP

I have an SVG string that I want to serve as JPEG using PHP. 我有一个要使用PHP用作JPEG的SVG字符串。 For this (and a lot more things) I am using Imagick. 为此,我使用了Imagick。 Below is the code from PHP: 以下是来自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. 我也尝试读取SVG文件,但没有成功。 Can someone please help? 有人可以帮忙吗?

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 操作系统:Windows 7 64 Bit
PHP: 5.5.6 的PHP:5.5.6
XAMPP: 1.8.3 XAMPP:1.8.3
Compiler: MSVC11 编译器:MSVC11
Architecture: x86 架构:x86
ImageMagick: ImageMagick-6.7.7-5-Q16-windows-dll (available here ) ImageMagick:ImageMagick-6.7.7-5-Q16-windows-dll( 在此处可用)
Imagick DLL: PHP5.5.x version from http://www.peewit.fr/imagick/ Imagick DLL: http ://www.peewit.fr/imagick/中的PHP5.5.x版本

Note: After I tried this, Apache started without any error messages. 注意:尝试此操作后,Apache启动时没有任何错误消息。 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). 我确实将ImageMagick安装在名称中没有空格的目录中(因为有人建议说某些安装可能有问题)。 In my case, I installed it to C:\\imagemagick. 就我而言,我将其安装到C:\\ imagemagick。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM