使用 ImageMagick 时如何修复 'Unable to annotate image' 错误?

How to fix 'Unable to annotate image' error when using ImageMagick?

我在 FreeBSD 11.0 服务器上使用 php 7.0.19 和 ImageMagick 6.9.6-4。我将 imagemagick 用于很多事情,但我才刚刚开始使用它在我的图像上叠加文本。我遇到的问题是,每当我尝试使用 annotateImage 功能时,都会抛出一个错误,简单地说 'Unable to annotate image.'

我已经在 Whosebug 上查看了很多与 annotateImage 相关的问题,并且我已经检查了文档以查看我是否可以自己解决问题,但我被卡住了。在其他 annotateImage 问题上,我看到有些人由于没有安装特定字体而遇到麻烦,这也可能是我的问题,但我尝试将字体文件(ttf 文件)放在与我的脚本相同的目录中我仍然遇到同样的问题。

运行 convert -list font returns 一个空结果,表明没有 imagemagick direct/default 访问的字体;但是,我在想,通过将字体文件包含在与我的脚本相同的目录中,我无论如何都可以让它工作。也许这是一个错误的假设?

这是我用于测试的代码:

$imagick = new Imagick('originalImage.jpg');
$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('finalImage.jpg');

我也尝试了其他不需要原始图像的示例脚本,但收到了同样的错误。例如,此脚本会产生相同的错误:

$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );
$image->newImage(800, 75, $pixel);
$draw->setFillColor('black');
$draw->setFont('ARIAL.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;

通过这个简单的测试,我希望在我的图像顶部添加 'The quick fox jumps over the lazy dog',但抛出了异常,错误消息只是说:无法注释图像。

关于如何解决错误的任何想法或建议?

谢谢!

通过在 imagemagick forum 上发布这个问题,我弄明白了。问题是我们的 ImageMagick 安装缺少 'freetype' 委托,这是从字体渲染文本所必需的。

谢谢!