【问题标题】:PHP Imagick Won't Read SVG filePHP Imagick 不会读取 SVG 文件
【发布时间】:2015-07-11 09:37:51
【问题描述】:

当我尝试加载 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 是什么”。错误是“该文件不存在。”
  • 文件确实存在,而且路径是正确的,因为我可以用file_get_contents()打开它。如果我这样做并使用$Vector-&gt;readimageblob(file_get_contents($path_to_image)); 打开它,我会收到错误Uncaught exception 'ImagickException' with message 'no decode delegate for this image format
  • 如果你真的读到了这个错误,那就是说 Imagick 在后台创建的 tmp 文件不存在,这与我的 SVG 文件不存在完全不同
  • 这可能是权限问题。是否创建了文件“/tmp/magick-aWsnhHLT”?并且您的网络服务器是否具有对 tmp 目录的读写权限?
  • 这不是权限问题,因为在使用 jpg/png 作为源文件时,相同的流程可以正常工作。当源文件是 svg 时它会中断。下面的解决方案对我有用。除了已经安装的php7.1-imagick 包之外,我还必须apt-get install imagemagick

标签: php svg imagick


【解决方案1】:

我知道我在这里聚会迟到了,但这个错误很可能是由于缺少库。

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

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

【讨论】:

  • 你拯救了这一天。 :)
  • 这是一个错误,因为提供的错误消息具有误导性和无益。
猜你喜欢
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
相关资源
最近更新 更多