百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> linux提示Warning: imagettftext(): Could not find/open font错
分享文章到:

linux提示Warning: imagettftext(): Could not find/open font错

发布时间:01/15 来源: 浏览: 关键词:
在php中imagettftext — 用 TrueType 字体向图像写入文本了,在其它版本中没有问题唯独在linux中使用imagettftext时出现imagettftext(): Could not find/open font错误了,具体我们来看解决办法。

(PHP 4, PHP 5, PHP 7)

imagettftext — 用 TrueType 字体向图像写入文本

说明

array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

使用 TrueType 字体将 指定的 text 写入图像。

很多情况下字体都放在脚本的同一个目录下。下面的小技巧可以减轻包含的问题。

<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>


最近一个项目在Ubuntn的apache服务器中验证码显示不出来,在CentOS的apache服务器中显示是正常的。以为是PHP配置的问题,但是GD库的开启的,相关服务也都正常,把显示验证码的方法单独拿出来测试,发现报错

Warning: imagettftext(): Could not find/open font

截取部分相关代码:

class Checkcode {
    //设置字体的地址
    private $font;
 
    function __construct() {
        $rand = rand(0, 1);
        if ($rand == 0) {
            $this->font = 'elephant.ttf';
        } else {
            $this->font = 'Vineta.ttf';
        }
    }
 
    /**
     * 生成文字
     */
    private function creat_font() {
        $x = $this->width / $this->code_len;
        for ($i = 0; $i < $this->code_len; $i++) {
            imagettftext($this->img, $this->font_size, rand(-30, 30), $x * $i + rand(0, 5), $this->height / 1.4, $this->font_color, $this->font, $this->code[$i]);
            if ($i == 0)
                $this->x_start = $x * $i + 5;
        }
    }
}

是imagettftext()这个函数报错了,找不到/打不开字体。应该是__construct()函数里指定字体文件出错了。

if ($rand == 0) {
            $this->font = 'elephant.ttf';
        } else {
            $this->font = 'Vineta.ttf';
 }

解决方法:

虽然上面代码指明的是字体文件在当前目录下,但是Ubuntn的apache服务器中还是要给字体文件指定明确的路径

$this->font = './elephant.ttf';    //相对路径
//或者
$this->font = '/var/www/html/project/elephant.ttf';    //绝对路径

这样验证码就可以正常显示了

从上面的函数用法来看就是打不开字体了,也就是一个非常简单路径问题了。

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有3人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板