PHP Imagick setFont不在web上工作但在控制台中工作

时间:2015-11-18 17:09:16

标签: php imagick

这是代码:     

/* Create Imagick objects */
$image = new \Imagick();
$draw = new \ImagickDraw();
$color = new \ImagickPixel('#000000');
$background = new \ImagickPixel('none'); // Transparent

/* Font properties */
$draw->setFont("annabelle");
$draw->setFontSize(80);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $text);

/* Create text */
$draw->annotation(0, $metrics['ascender'], $text);

/* Create image */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], background);
$image->setImageFormat('png');
$image->drawImage($draw);

/* Save image */
file_put_contents('imagick_test.png', $image);
?>

和ImageMagick配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<typemap>
   <include file="type-dejavu.xml" />
   <include file="type-ghostscript.xml" /> 
   <include file="type-windows.xml" />
   <type name="annabelle" family="annabelle" glyphs="/home/nginx/testing/annabelle.ttf" />
</typemap>

如果我通过以下方式在控制台模式下调用:php -f test.php,那就没关系。但是当我通过网络界面访问:http://test-srv/testing/test.php时,它会引发异常:

致命错误:未捕获的异常'ImagickException',消息'路径不存在:/ home / nginx / testing / annabelle'在/home/nginx/testing/test.php:15堆栈跟踪:#0 / home / nginx / testing / test.php(15):第15行/home/nginx/testing/test.php中的ImagickDraw-&gt; setfont('annabelle')#1 {main}

我尝试使用setFontFamily()而不是setFont(),如下所示:

...
/* Font properties */
$draw->setFontFamily("annabelle");
$draw->setFontSize(80);
...

或者使用字体文件代替setFont()中的字体名称,如下所示:

...
/* Font properties */
$draw->setFont("annabelle.ttf");
$draw->setFontSize(80);
...

像这样创建的图像: Wrong one

如果我在cosole中运行代码,那么正确的应该是这样的: Right one

1 个答案:

答案 0 :(得分:0)

好吧,我明白了。

我已经将PHP从5.5升级到5.6,比我发现Imagick模块已禁用所以我用PHP_TARGET =“php5-6”重新出现了Imagick(我正在运行gentoo)。但我忘了重启php-fpm服务。重启服务后,它运行正确。