Question

I have attempted to create a png with imagemagick with the following code.

<?php
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );

/* New image */
$image->newImage(800, 75, $pixel);

/* Black text */
$draw->setFillColor('black');

/* Font properties */
$draw->setFont('Oxin_free_promo.ttf');
$draw->setFontSize( 30 );

/* Create text */
$image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');

/* Give image a format */
$image->setImageFormat('png');

/* Output the image with headers */
header('Content-type: image/png');
echo $image;

But I keep getting the errors below. /usr/local/zend/tmp/ is chmod'd to be 777 and owned by daemon:zend and magick-* doesnt exist. Any ideas?

httpd: FreeType library is not available `/Users/gareth.williams/Sites/_tests/texttoimg/Oxin_free_promo.ttf'.
httpd: Postscript delegate failed `/usr/local/zend/tmp/magick-tmfNBDIx': No such file or directory.
PHP Fatal error:  Uncaught exception 'ImagickException' with message 'Unable to annotate image'
Was it helpful?

Solution

Look at the error messages given, it says the problem right there.

"FreeType library is not available" - ImageMagick uses the FreeType library to render fonts, you have given it a TTF to use, and it says FreeType is not installed, so it can not use the font. Install FreeType, then reinstall/recompile ImageMagick to get FreeType support.

The second error is probably ImageMagick trying to fallback to rendering the font using its postscript rasterisation support, usually this is via ghostscript (gs), hence "Postscript delegate failed". Either ghostscript is not available, or the path for the temporary file it asks ghostscript to render too/from is not writable. Could be fixed by installing ghostscript and making sure that temporary directory has full write access, as people mention in the comments to the question.

To confirm these causes, use the -list option of convert.

  • convert -list format : This will show you the supported file types, look for TTF, make sure it has at least r-- status, it should say something like "TTF* TTF r-- TrueType font (Freetype 2.2.1)" if FreeType is installed correctly.

  • convert -list delegate : This will show handling of delegated types and the command line used. It will be using one of the ps converters as a fallback.

As I say, easiest fix is to install FreeType, then recompile/reinstall ImageMagick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top