PHP Imagick无法读取SVG文件

7
当我尝试加载SVG文件时,出现了以下错误信息。
Fatal error: Uncaught exception 'ImagickException' with message
'unable to open file `/tmp/magick-aWsnhHLT': No such file or directory
 @ error/constitute.c/ReadImage/583'
in /var/www/file.php:250
Stack trace:
#0 /var/www/file.php(250): Imagick->readimage('/var/www/ima...')
#1 {main} thrown in /var/www/file.php on line 250

这段代码是这样的

$Vector = new Imagick();
$Vector->readimage("$_SERVER[DOCUMENT_ROOT]/images/Vector.svg");

还有这个SVG文件

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="59.760000pt" height="71.520000pt" viewBox="0 0 59.760000 71.520000"
 preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.11, written by Peter Selinger 2001-2013
</metadata>
<g transform="translate(0.000000,71.520000) scale(0.024000,-0.024000)"
fill="#ffffff" stroke="none">
<path d="M1367 2540 c-7 0 -30 -9 -52 -21 -22 -12 -52 -34 -67 -51 -21 -21
-28 -40 -28 -68 0 -22 5 -50 12 -64 6 -14 21 -32 34 -41 22 -14 23 -14 17 20
-3 18 -3 46 1 60 4 14 15 34 26 45 11 11 33 20 48 20 18 0 38 -10 53 -26 18
-19 24 -37 24 -69 0 -32 -6 -50 -25 -70 -21 -22 -22 -28 -10 -35 8 -5 27 -10
41 -10 21 0 30 7 40 33 8 17 14 58 13 89 -1 38 -8 67 -20 85 -10 15 -24 40
-31 56 -7 15 -24 32 -38 37 -15 6 -32 10 -38 10z"/>
<path d="M976 2520 c-19 0 -32 -10 -53 -42 -20 -32 -28 -60 -31 -105 -3 -41 1
-78 13 -112 9 -28 20 -51 23 -51 4 0 15 7 25 14 9 7 17 17 17 21 0 5 -10 19
-22 33 -17 18 -23 36 -23 71 0 26 6 55 14 64 7 10 24 17 37 17 13 0 32 -9 42
-20 9 -11 22 -42 28 -68 l10 -47 18 33 c13 25 16 45 10 75 -4 29 -18 53 -44
79 -24 25 -47 38 -64 38z"/>
<path d="M1473 2050 c-5 0 -28 -9 -52 -20 -24 -11 -71 -40 -105 -64 -34 -23
-76 -48 -95 -54 -19 -7 -52 -12 -74 -12 -23 0 -60 9 -84 21 -24 11 -61 42 -83
67 -33 39 -42 44 -50 32 -5 -8 -10 -35 -10 -61 0 -37 -11 -65 -64 -158 -51
-88 -67 -127 -77 -184 -9 -53 -30 -104 -75 -192 -35 -66 -75 -156 -90 -200
-21 -62 -28 -106 -32 -196 l-5 -116 84 -70 c46 -39 120 -100 164 -135 43 -35
90 -78 102 -97 13 -19 23 -46 23 -61 0 -15 -10 -35 -22 -47 -13 -11 -36 -25
-53 -30 -16 -5 -30 -15 -30 -21 0 -5 15 -31 32 -56 18 -25 47 -55 64 -65 17
-11 61 -25 96 -31 39 -8 96 -10 142 -7 42 4 103 13 135 22 33 9 93 35 134 58
41 23 108 74 148 112 l74 70 0 151 c0 134 2 153 18 167 12 11 36 17 65 17 46
0 46 0 58 43 8 28 10 93 6 192 -4 109 -11 170 -27 223 -11 41 -32 97 -46 125
-15 29 -42 71 -61 95 -25 31 -40 64 -54 123 -11 45 -44 132 -73 193 -30 61
-59 123 -65 139 -6 15 -14 27 -18 27z"/>
</g>
</svg>

Imagick不能打开SVG文件吗?不过EPS文件似乎没有问题。非常感谢您的帮助。

错误不在于“我不知道什么是SVG”,而在于“该文件不存在”。 - kainaw
文件确实存在,路径也正确,因为我可以使用file_get_contents()打开它。如果我这样做并使用$Vector->readimageblob(file_get_contents($path_to_image));打开它,我会收到错误消息Uncaught exception 'ImagickException' with message 'no decode delegate for this image format - Brian Leishman
1
如果您仔细阅读错误提示,它所说的是 Imagick 在后台创建的 tmp 文件不存在,这与我的 SVG 文件不存在完全不同。 - Brian Leishman
这很可能是一个权限问题。文件 '/tmp/magick-aWsnhHLT' 是否被创建?您的 Web 服务器是否有读写 tmp 目录的权限? - Danack
1
这不是权限问题,因为当使用jpg/png作为源文件时,相同的流程可以无错误地工作。当源文件是svg时,它会出现问题。 下面的解决方案对我有用。除了已安装的php7.1-imagick软件包外,我还需要apt-get install imagemagick - antriver
1个回答

20

我知道我来晚了,但这个错误很可能是由于缺少库造成的。

尝试根据发行版安装libmagickcore5-extra或libmagickcore-6.q16-2-extra

通过这个错误报告找到的(虽然不是真正的错误):https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=576627


1
你救了大家一命。 :) - Mihail Minkov
由于提供的错误信息具有误导性和无效性,因此这是一个错误。 - TRiG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接