且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

php在linux下call to undefined function imagettftext()

更新时间:2022-09-16 13:20:04

imagettftext和imagefttext两个函数在生成验证码或需要向图片写入文字时候常用,在linux环境下只开启gd库是不够的。

这两个函数均需要 FreeType 库支持,在函数文档中也可以找到

php在linux下call to undefined function imagettftext()

所以当发现调用时报错提示函数不存在,都是因为编译时没有指定freetype库激活造成的。如果需要则必需重新编译gd库并激活freetype支持。


freetype库是三方库,需要单独安装,如果系统中已经安装则无需安装

查看命令:

1
rpm -ip freetype

1
whereis freetype

下载地址: https://www.freetype.org/download.html

***使用.net域名下的,.org很容易造成超时。

这里以:

https://sourceforge.net/projects/freetype/files/freetype2/2.7.1/freetype-2.7.1.tar.gz/download

为例:

1
2
3
4
5
tar -zxf freetype-2.7.1.tar.gz
cd freetype-2.7.1/builds/unix
./configure --prefix=/usr/local/freetype/2.7.1 --without-harfbuzz
make
make install

说明:如果不进入builds/unix目录下编译会提示:make: Nothing to be done for `unix'. 当然也没有什么影响。

安装freetype需要包:

 external
 bzip2
 libpng
 harfbuzz
可以使用yum安装,但harfbuzz在yum中没有所以需要编译安装,而编译时又需要freetype包,所以可以不安装这个包,然后在freetype编译时增加--without-harfbuzz即可。


安装好freetype后即可编译gd库,官方文档中也有说明:

php在linux下call to undefined function imagettftext()

所以编译gd库需要增加--with-freetype-dir=/usr/local/freetype/2.7.1/ --enable-gd-native-ttf

注意:如果freetype时默认安装无需指定目录。


编译前一定要记得 make clean 清除上次的编译内容,尤其是已经编译安装过的。

./configure 配置后可以查看  Configuring extensions 下面是否有相关成功记录:

php在linux下call to undefined function imagettftext()


然后

1
2
make
make install

编译好后则可以查看phpinfo()

php在linux下call to undefined function imagettftext()

出现这个即安装成功,可以使用这两个函数。

本文转自  ttlxihuan    51CTO博客,原文链接:http://blog.51cto.com/php2012web/1969401