PHP 致命错误:调用未定义的函数 imagettftext()
为什么我在第 29 行收到错误 PHP Fatal error: Call to undefined function imagettftext()
?
<?php
ob_start();
session_start();
$strings = '123456789';
$i = 0;
$characters = 6;
$code = '';
while ($i < $characters)
{
$code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
$i++;
}
$_SESSION['captcha'] = $code;
//generate image
$im = imagecreatetruecolor(124, 40);
$foreground = imagecolorallocate($im, 0, 0, 0);
$shadow = imagecolorallocate($im, 173, 172, 168);
$background = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 200, 200, $background);
// use your own font!
$font = 'monofont.ttf';
//draw text:
imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);
// prevent client side caching
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//send image to browser
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>`
我的 PHP 信息:
Why am I getting the error PHP Fatal error: Call to undefined function imagettftext()
on line 29?
<?php
ob_start();
session_start();
$strings = '123456789';
$i = 0;
$characters = 6;
$code = '';
while ($i < $characters)
{
$code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
$i++;
}
$_SESSION['captcha'] = $code;
//generate image
$im = imagecreatetruecolor(124, 40);
$foreground = imagecolorallocate($im, 0, 0, 0);
$shadow = imagecolorallocate($im, 173, 172, 168);
$background = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 200, 200, $background);
// use your own font!
$font = 'monofont.ttf';
//draw text:
imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);
// prevent client side caching
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//send image to browser
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>`
My PHP Info:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 PHP 手册条目
imagettftext()
:
您的 PHP 构建中必须缺少一个或两个必需的库。
According to the PHP manual entry for
imagettftext()
:You must be missing one or both of the required libraries in your PHP build.
我在我的 docker
php:7-fpm
环境中解决了同样的问题,我在这里发布了解决方案:如果使用
Dockerfile
来设置环境如果想添加 FreeType 模块在现有容器上:
I solve the same issue on my docker
php:7-fpm
enviroment, and I post the solution here:If using
Dockerfile
to setup the enviromentIf want to add the FreeType module on an exist container:
对于使用 PHP 7.4 的 Docker 容器,请使用以下命令安装扩展:
使用之前的方法会导致:
相关评论。
For a Docker container with PHP 7.4, use these commands to install the extension:
Using the previous approach results in:
Relevant comment.
只需在
php/ext/gd
文件夹下重新编译扩展 gd.soJust recompile extension gd.so, under folder
php/ext/gd
对于具有 Apache 7.3 的 Docker 容器,以下内容对我有用:
它基本上是 Alfred Huang 提供的清理版本 - 没有
mcrypt
和mbstring
。For a Docker container with an Apache 7.3 , the following is working for me:
It's basically a cleaned up version as provided by Alfred Huang - without
mcrypt
andmbstring
.对于使用 PHP8 的容器镜像:
For container image using PHP8: