Serving SVG as JPEG using Imagick and PHP

时间:2022-11-11 21:15:53

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字符串,我希望使用PHP作为JPEG。为此(以及更多的东西)我正在使用Imagick。以下是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?

我对发生的事情毫无头绪。我也尝试过阅读SVG文件,但没有成功。有人可以帮忙吗?

1 个解决方案

#1


0  

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/

操作系统:Windows 7 64位PHP:5.5.6 XAMPP:1.8.3编译器:MSVC11架构:x86 ImageMagick:ImageMagick-6.7.7-5-Q16-windows-dll(可在此处)Imagick DLL:PHP5.5.x版本来自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.

注意:尝试此操作后,Apache启动时没有任何错误消息。我确实将ImageMagick安装在名称中没有空格的目录中(正如有人建议某些安装可能存在问题)。在我的例子中,我将它安装到C:\ imagemagick。

#1


0  

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/

操作系统:Windows 7 64位PHP:5.5.6 XAMPP:1.8.3编译器:MSVC11架构:x86 ImageMagick:ImageMagick-6.7.7-5-Q16-windows-dll(可在此处)Imagick DLL:PHP5.5.x版本来自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.

注意:尝试此操作后,Apache启动时没有任何错误消息。我确实将ImageMagick安装在名称中没有空格的目录中(正如有人建议某些安装可能存在问题)。在我的例子中,我将它安装到C:\ imagemagick。