open - php gd create image from text
Error fatal de PHP: llamada a una funciĆ³n no definida imagettftext() (3)
De acuerdo con la entrada del manual de PHP para imagettftext()
:
Esta función requiere tanto la biblioteca GD como la biblioteca »FreeType.
Debe faltar una o las dos bibliotecas requeridas en su compilación de PHP.
¿Por qué recibo el error PHP Fatal error: Call to undefined function imagettftext()
Error PHP Fatal error: Call to undefined function imagettftext()
en la línea 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);
?>`
Mi información de PHP:
Resuelvo el mismo problema en mi entorno docker php:7-fpm
, y php:7-fpm
la solución aquí:
Si usa Dockerfile
para configurar el entorno
# 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
Si desea agregar el módulo FreeType en un contenedor existente:
# 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
Solo recompile la extensión gd.so, en la carpeta php/ext/gd
./configure --with-php-config=/usr/local/php5/bin/php-config --with-freetype-dir=/usr/ --enable-gd-native-ttf