Page 1 of 1

Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T08:44:02-07:00
by caliguian
I am using php 7.0.19 and ImageMagick 6.9.6-4 on a FreeBSD 11.0 server. I use imagemagick for quite a few things, but I am just getting started with using it to overlay text on top of my images. The problem I'm having is that any time I try to use the annotateImage functionality an error is thrown that simply says 'Unable to annotate image.'

I have looked through quite a few questions related to annotateImage and I have checked the docs to see if I could resolve the issue on my own, but I'm stuck. On other annotateImage questions I have seen that some people have trouble due to not having a specific font installed, and that may be my problem as well, but I have tried placing a font file (the ttf file) in the same directory as my script and I am still having the same issue.

Running convert -list font returns an empty result, indicating that there are no fonts that imagemagick has direct/default access to; however, I was thinking that by including the font file in the same directory as my script I could make it work anyway. Perhaps this is a mistaken assumption?

Here is the code I am using for my test:

Code: Select all

$imagick = new Imagick('originalImage.jpg');
$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFont('/file/location/myfont.ttf');
$draw->setFontSize(20);
$imagick->annotateImage($draw, 20, 100, 0, 'The quick fox jumps over the lazy dog');
$imagick->drawImage($draw);
$imagick->writeImage('finalImage.jpg');
I have also tried other example scripts that don't require an original image, and received the same error. For example, this script produces the same error:

Code: Select all

$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );
$image->newImage(800, 75, $pixel);
$draw->setFillColor('black');
$draw->setFont('/file/location/myfont.ttf');
$draw->setFontSize( 30 );
$image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
With these simple tests I was expecting to add 'The quick fox jumps over the lazy dog' to an image, but instead an exception is thrown and the error message simply says: Unable to annotate image.

Any ideas or suggestions on how I can resolve the error?

Thank you!

Re: Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T09:19:50-07:00
by fmw42
This works fine for me. The image lena.png and the font file arial.ttf are both in the same directory as my php file

Code: Select all

<?php
$imagick = new Imagick('lena.png');
$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFont('arial.ttf');
$draw->setFontSize(20);
$imagick->annotateImage($draw, 20, 100, 0, 'The quick fox jumps over the lazy dog');
$imagick->drawImage($draw);
$imagick->writeImage('lena_text.jpg');
?>	
I suspect that you may not have a good Imagick install or your ImageMagick version 6 or 7 is incompatible with your PHP Imagick or perhaps you do not have the freetype delegate library installed.

What do you get from

Code: Select all

<?php
echo "<pre>";
system("type -a convert");  
echo "</pre>";
?> 
and from

Code: Select all

<?php
exec("convert -version",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

Re: Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T09:49:13-07:00
by caliguian
Thanks for trying to help out with this. The code block gives me:
convert is /usr/local/bin/convert
The second gives me:
Version: ImageMagick 6.9.6-4 Q16 amd64 2017-04-04 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib fontconfig jbig jng jp2 jpeg lcms ltdl lzma png webp xml zlib

Re: Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T09:51:59-07:00
by fmw42
Your ImageMagick is missing freetype delegate that is needed to render text from fonts. See https://www.freetype.org. How did you install ImageMagick? Where did it come from?

Re: Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T09:58:11-07:00
by caliguian
I didn't install it myself, so I'm not sure where it came from (the person that takes care of our servers installed it).

Should I just ask them to update it and include the freetype delegate? It sounds like that may be what I need to do.

Thank you again for your help. I've spent several hours across the past week looking for potential solutions, and this gives me a good idea of how to start working on this from here.

Re: Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T10:00:49-07:00
by fmw42
Yes, get your IT people to either install the freetype delegate into ImageMagick or upgrade ImageMagick with the delegate included. ImageMagick 6.9.6-4 is rather old, though on Linux servers the date (2017-04-04) of the patch is more important. So your version is about 2 years old if properly patched.

Re: Can you help with an 'Unable to annotate image' error?

Posted: 2019-05-23T10:03:30-07:00
by caliguian
Sounds good. Thanks!