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

时间:2011-09-03 03:25:04

标签: php freetype php-gd freetype2

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

enter image description here

enter image description here

5 个答案:

答案 0 :(得分:35)

根据PHP manual entry for imagettftext()

  

此功能需要GD库和»FreeType库。

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

答案 1 :(得分:25)

我在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

答案 2 :(得分:3)

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

  

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

答案 3 :(得分:2)

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

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

使用先前的方法会导致:

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

Relevant comment.

答案 4 :(得分:1)

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

mcrypt

基本上,这是Alfred Huang所提供的清理版本-没有mbstring def Hough_Circles(image,dp,minDist,param1,param2,minRadius,maxRadius): #circles = [] try: circles = cv.HoughCircles(image, cv.HOUGH_GRADIENT, dp, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius) if(isinstance(circles,np.ndarray)): circles = np.uint16(np.around(circles)) else: circles = [[["BAD READ"]]] except: print("no circle found") return circles