Page 1 of 1

Imagick setFont not working in web but works in console

Posted: 2015-11-18T10:59:10-07:00
by harley24
Here's my code:

Code: Select all

<?php
/* 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);
?>
And ImageMagick configuration file:

Code: Select all

<?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>
If I call this in console mode by:

Code: Select all

nginx@test-srv ~/testing $ php -f test.php
, it's OK. But when I accessing by web interface: http://test-srv/testing/test.php, It raise an exception:

Fatal error: Uncaught exception 'ImagickException' with message 'The path does not exist: /home/nginx/testing/annabelle' in /home/nginx/testing/test.php:15 Stack trace: #0 /home/nginx/testing/test.php(15): ImagickDraw->setfont('annabelle') #1 {main} thrown in /home/nginx/testing/test.php on line 15

I tried use setFontFamily() instead of setFont() like this:

Code: Select all

...
/* Font properties */
$draw->setFontFamily("annabelle");
$draw->setFontSize(80);
...
Or using the font file instead using font name in setFont() like this:

Code: Select all

...
/* Font properties */
$draw->setFont("annabelle.ttf");
$draw->setFontSize(80);
...
The image created but not using the font specified like this:
Error one
Error one
imagick_test.png (1.39 KiB) Viewed 11820 times
If I run the code in console, I get the right one like this :
Right one
Right one
imagick_test (1).png (6.32 KiB) Viewed 11820 times

Re: Imagick setFont not working in web but works in console

Posted: 2015-11-18T12:17:15-07:00
by fmw42
Imagick apparently does not know about those fonts that Imagemagick has access to through the type.xml file. Try specifying the full path to your font file in your Imagick code.

Re: Imagick setFont not working in web but works in console

Posted: 2015-11-19T17:40:44-07:00
by harley24
fmw42 wrote:Imagick apparently does not know about those fonts that Imagemagick has access to through the type.xml file. Try specifying the full path to your font file in your Imagick code.
I change the code to using absolutely path, but still get the wrong image. The code after change is:

Code: Select all

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

Re: Imagick setFont not working in web but works in console

Posted: 2015-11-19T17:46:26-07:00
by fmw42
Sorry, I do not know Imagick. Are you sure that is the correct full path?

Re: Imagick setFont not working in web but works in console

Posted: 2015-11-19T17:50:26-07:00
by harley24
Well, I get the answer.

I've upgrade php from 5.5 to 5.6, than I found Imagick module disabled so I re-complied Imagick with PHP_TARGET="php5-6" (I'm running gentoo). But I forget restart php-fpm service. After I restart the service, it runs right.

Re: Imagick setFont not working in web but works in console

Posted: 2015-11-19T17:51:38-07:00
by harley24
fmw42 wrote:Sorry, I do not know Imagick. Are you sure that is the correct full path?
The path is OK, and I found what's the problem. Thanks for your reply. :D