PHP Imagick setFont 在网络中不工作但在控制台中工作
PHP Imagick setFont not working in web but works in console
代码如下:
/* 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,它引发了一个异常:
致命错误:/home/nginx/testing/test.php 中的消息 'The path does not exist: /home/nginx/testing/annabelle' 未捕获异常 'ImagickException':15 堆栈跟踪:#0 /home/nginx/testing/test.php(15): ImagickDraw->setfont('annabelle') #1 {main} throw in /home/nginx/testing/test.php on line 15
我试过像这样使用 setFontFamily() 而不是 setFont():
...
/* Font properties */
$draw->setFontFamily("annabelle");
$draw->setFontSize(80);
...
或者使用字体文件而不是像这样在 setFont() 中使用字体名称:
...
/* Font properties */
$draw->setFont("annabelle.ttf");
$draw->setFontSize(80);
...
创建的图像是这样的:
如果我 运行 cosole 中的代码,正确的应该是这样的:
嗯,我知道答案了。
我已经将 php 从 5.5 升级到 5.6,然后我发现 Imagick 模块被禁用,所以我用 PHP_TARGET="php5-6" 重新出现 Imagick(我是 运行 gentoo)。但是我忘记重启php-fpm服务了。我重新启动服务后,它运行正常。
代码如下:
/* 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,它引发了一个异常:
致命错误:/home/nginx/testing/test.php 中的消息 'The path does not exist: /home/nginx/testing/annabelle' 未捕获异常 'ImagickException':15 堆栈跟踪:#0 /home/nginx/testing/test.php(15): ImagickDraw->setfont('annabelle') #1 {main} throw in /home/nginx/testing/test.php on line 15
我试过像这样使用 setFontFamily() 而不是 setFont():
...
/* Font properties */
$draw->setFontFamily("annabelle");
$draw->setFontSize(80);
...
或者使用字体文件而不是像这样在 setFont() 中使用字体名称:
...
/* Font properties */
$draw->setFont("annabelle.ttf");
$draw->setFontSize(80);
...
创建的图像是这样的:
如果我 运行 cosole 中的代码,正确的应该是这样的:
嗯,我知道答案了。
我已经将 php 从 5.5 升级到 5.6,然后我发现 Imagick 模块被禁用,所以我用 PHP_TARGET="php5-6" 重新出现 Imagick(我是 运行 gentoo)。但是我忘记重启php-fpm服务了。我重新启动服务后,它运行正常。