Imagick setFont not working in web but works in console

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
harley24
Posts: 4
Joined: 2015-11-18T10:47:28-07:00
Authentication code: 1151

Imagick setFont not working in web but works in console

Post 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 11634 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 11634 times
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
harley24
Posts: 4
Joined: 2015-11-18T10:47:28-07:00
Authentication code: 1151

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

Post 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);
...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Sorry, I do not know Imagick. Are you sure that is the correct full path?
harley24
Posts: 4
Joined: 2015-11-18T10:47:28-07:00
Authentication code: 1151

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

Post 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.
harley24
Posts: 4
Joined: 2015-11-18T10:47:28-07:00
Authentication code: 1151

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

Post 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
Post Reply