Imagick:查询字体规格失败

发布于 2024-11-27 08:38:55 字数 1087 浏览 1 评论 0原文

我正在开发(应该是)一个简单的脚本,它将向图像添加文本。在多次检查我的脚本以查找任何错误之后,我最终决定尝试运行 php.net 上的示例,但我遇到了相同的、不伦不类的错误:“无法查询字体指标”。这是代码:

/* Text to write */
$text = "Hello World!";

/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent

/* Font properties */
$draw->setFont('Arial');
$draw->setFontSize(50);
$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('/tmp/file.png', $image);

我一生都无法通过谷歌找到有关此错误的任何信息。我也找不到有关此方法或失败的潜在原因的足够文档。基本上,我很困惑。如果有人能够提供见解或修复,我们将不胜感激。

ImageMagic 版本:ImageMagick 6.6.5-10 2011-04-06 Q16

Imagick 模块版本:3.1.0b1

I'm working on (what should be) a simple script that will add text to an image. After going over my script several times looking for any mistakes I finally decided to try running a sample from php.net and I encountered the same, nondescript, error: "Failed to query the font metrics". Here's the code:

/* Text to write */
$text = "Hello World!";

/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent

/* Font properties */
$draw->setFont('Arial');
$draw->setFontSize(50);
$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('/tmp/file.png', $image);

I can not for the life of me find any information via google about this error. Nor can I find adequate documentation on this method or potential causes of failure. Basically, I'm stumped. If anyone could provide insight or a fix it would be greatly appreciated.

ImageMagic version: ImageMagick 6.6.5-10 2011-04-06 Q16

Imagick module version: 3.1.0b1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-12-04 08:38:55

你试过cli版本吗?服务器上安装了imagemagick吗?如果是
然后运行命令,例如

system('convert -background lightblue -fill blue \
      -font Candice -pointsize 72 label:Anthony \
      label.gif  ');

在运行脚本后查看服务器中是否有名为 label.gif 的图像。
供您参考http://www.imagemagick.org/Usage/text/

have you tried cli version ? is imagemagick installed on server ? if yes
then run a command like

system('convert -background lightblue -fill blue \
      -font Candice -pointsize 72 label:Anthony \
      label.gif  ');

see if you have imagenamed label.gif in server after running script.
for your reference http://www.imagemagick.org/Usage/text/

作妖 2024-12-04 08:38:55

对于那些遇到类似问题的人来说,PHP Imagick 异常并不总是最具描述性。我本来可以节省大量时间来检查服务器上安装的 Image Magic 应用程序的输出。只是要记住一些事情。要查看当前安装的委托(模块)的列表,请使用命令 convert -list configure 并检查输出中以“DELEGATES”开头的行。如果您遇到同样的错误,我建议您首先检查此处。我发现我缺少 freetype 和 Ghostscript 代表。安装依赖项并快速重新编译 ImageMagick 后,一切都变得非常顺利。

For those having similar issues, the PHP Imagick exceptions aren't always the most descriptive. I could have saved myself a lot of time examining the output from the Image Magic application installed on the server first. Just something to keep in mind. To view a list of the currently installed delegates(modules) use the command convert -list configure and examine the line that starts with "DELEGATES" from the output. If you encounter the same error, I recommend checking here first. I found I was missing the freetype and ghostscript delegates. After installing the dependancies and a quick recompile of ImageMagick everything works like a charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文