PHP Fatal error: Call to undefined function imagettftext()
Asked Answered
A

6

38

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

Amadou answered 3/9, 2011 at 3:25 Comment(2)
Because ... there's no such function defined?Lachrymal
Googling "imagettftext" brings up ... directadmin.com/forum/showthread.php?t=6410Lachrymal
M
43

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.

Metic answered 3/9, 2011 at 3:29 Comment(3)
@Jess McKenzie: Please show us the "Configure Command" part of phpinfo() too.Metic
Where to add these libraries? I am using heroku with custom buildpack.Derision
Just in case someone is searching for this and is using WHM, run /scripts/easyapache and select TTF Freetype from the Exhaustive Options List and recompile.Airfield
M
41

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
Metchnikoff answered 13/2, 2016 at 14:37 Comment(6)
Amazing, just the answer I was looking for.Corundum
great, but I need to take away the last command docker-php-ext-install -- is that a copy-paste-typo?Pasta
@Pasta well, it's a mistake, I've removed that, thank you.Metchnikoff
Is vim really needed for imagettftext support or just a copy-paste remainder? Also is opcache related to the inquiry?Abeokuta
@YoanTournade Thank you for pointing that out, the answer is updated.Metchnikoff
Error: libpng12-dev no candidate to install.Unchancy
D
6

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.

Debarath answered 30/1, 2020 at 6:35 Comment(1)
Also works for docker containers with php:8-apache.Salyers
R
4

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

Replay answered 4/12, 2014 at 16:41 Comment(0)
V
4

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.

Villainous answered 7/10, 2019 at 19:10 Comment(1)
Also works fine with php:7.3-fpm. This supposed to work fine with all php7.3 variations I guess.Collazo
M
0

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
Mayst answered 22/4 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.