PHP 致命错误:调用未定义的函数 imagettftext()

发布于 2024-12-02 12:33:55 字数 1380 浏览 8 评论 0原文

为什么我在第 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:

enter image description here

enter image description here

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

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

发布评论

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

评论(6

不醒的梦 2024-12-09 12:33:55

根据 PHP 手册条目 imagettftext()

此函数需要 GD 库和 » FreeType 库。

您的 PHP 构建中必须缺少一个或两个必需的库。

According to the PHP manual entry for imagettftext():

This function requires both the GD library and the » FreeType library.

You must be missing one or both of the required libraries in your PHP build.

吻安 2024-12-09 12:33:55

我在我的 docker php:7-fpm 环境中解决了同样的问题,我在这里发布了解决方案:


如果使用 Dockerfile 来设置环境

# more Dockerfile  
FROM php:fpm 
RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
        libmcrypt-dev \
        libpng12-dev \
        libjpeg-dev \
        libpng-dev
    && docker-php-ext-install iconv mcrypt \
    && docker-php-ext-configure gd \
        --enable-gd-native-ttf \
        --with-freetype-dir=/usr/include/freetype2 \
        --with-png-dir=/usr/include \
        --with-jpeg-dir=/usr/include \
    && docker-php-ext-install gd \
    && docker-php-ext-install mbstring \
    && docker-php-ext-enable gd

如果想添加 FreeType 模块在现有容器上:

# on docker host machine
docker exec -it $FPM_CONTAINER bash

>>>>

# inside the container
apt-get install -y \
        libfreetype6-dev \
        libmcrypt-dev \
        libpng12-dev \
        libjpeg-dev \
        libpng-dev
docker-php-ext-configure gd \
        --enable-gd-native-ttf \
        --with-freetype-dir=/usr/include/freetype2 \
        --with-png-dir=/usr/include \
        --with-jpeg-dir=/usr/include \
    && docker-php-ext-install gd

exit

<<<<

docker restart $FPM_CONTAINER

I solve the same issue on my docker php:7-fpm enviroment, and I post the solution here:


If using Dockerfile to setup the enviroment

# more Dockerfile  
FROM php:fpm 
RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
        libmcrypt-dev \
        libpng12-dev \
        libjpeg-dev \
        libpng-dev
    && docker-php-ext-install iconv mcrypt \
    && docker-php-ext-configure gd \
        --enable-gd-native-ttf \
        --with-freetype-dir=/usr/include/freetype2 \
        --with-png-dir=/usr/include \
        --with-jpeg-dir=/usr/include \
    && docker-php-ext-install gd \
    && docker-php-ext-install mbstring \
    && docker-php-ext-enable gd

If want to add the FreeType module on an exist container:

# on docker host machine
docker exec -it $FPM_CONTAINER bash

>>>>

# inside the container
apt-get install -y \
        libfreetype6-dev \
        libmcrypt-dev \
        libpng12-dev \
        libjpeg-dev \
        libpng-dev
docker-php-ext-configure gd \
        --enable-gd-native-ttf \
        --with-freetype-dir=/usr/include/freetype2 \
        --with-png-dir=/usr/include \
        --with-jpeg-dir=/usr/include \
    && docker-php-ext-install gd

exit

<<<<

docker restart $FPM_CONTAINER
栖竹 2024-12-09 12:33:55

对于使用 PHP 7.4 的 Docker 容器,请使用以下命令安装扩展:

docker-php-ext-configure gd --with-freetype
docker-php-ext-install gd

使用之前的方法会导致:

configure: error: unrecognized options: --with-freetype-dir

相关评论。

For a Docker container with PHP 7.4, use these commands to install the extension:

docker-php-ext-configure gd --with-freetype
docker-php-ext-install gd

Using the previous approach results in:

configure: error: unrecognized options: --with-freetype-dir

Relevant comment.

凑诗 2024-12-09 12:33:55

只需在 php/ext/gd 文件夹下重新编译扩展 gd.so

<代码>./配置
--with-php-config=/usr/local/php5/bin/php-config --with-freetype-dir=/usr/ --enable-gd-native-ttf

Just recompile extension gd.so, under folder php/ext/gd

./configure
--with-php-config=/usr/local/php5/bin/php-config --with-freetype-dir=/usr/ --enable-gd-native-ttf

-小熊_ 2024-12-09 12:33:55

对于具有 Apache 7.3 的 Docker 容器,以下内容对我有用:

FROM php:7.3.10-apache

RUN apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev \
  && docker-php-ext-configure gd \
     --with-freetype-dir=/usr/include/freetype2 \
     --with-png-dir=/usr/include \
     --with-jpeg-dir=/usr/include \
  && docker-php-ext-install gd

它基本上是 Alfred Huang 提供的清理版本 - 没有 mcryptmbstring

For a Docker container with an Apache 7.3 , the following is working for me:

FROM php:7.3.10-apache

RUN apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev \
  && docker-php-ext-configure gd \
     --with-freetype-dir=/usr/include/freetype2 \
     --with-png-dir=/usr/include \
     --with-jpeg-dir=/usr/include \
  && docker-php-ext-install gd

It's basically a cleaned up version as provided by Alfred Huang - without mcrypt and mbstring.

ゞ花落谁相伴 2024-12-09 12:33:55

对于使用 PHP8 的容器镜像:

FROM php:8.3.0-fpm-alpine

RUN apk update && \
    apk add freetype-dev \
            libmcrypt-dev \
            libpng-dev \
            libjpeg \
            libpng-dev && \ 
    docker-php-ext-configure gd --enable-gd --with-freetype && \ 
    docker-php-ext-install gd

For container image using PHP8:

FROM php:8.3.0-fpm-alpine

RUN apk update && \
    apk add freetype-dev \
            libmcrypt-dev \
            libpng-dev \
            libjpeg \
            libpng-dev && \ 
    docker-php-ext-configure gd --enable-gd --with-freetype && \ 
    docker-php-ext-install gd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文