linux提示Warning: 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';    //绝对路径

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

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

时间: 2024-08-04 02:15:31

linux提示Warning: imagettftext(): Could not find/open font错误的相关文章

linux编译安装mysql提示Warning: prerequisite DBD::mysql 3 not found错误

在编译percona-toolkit-2.2.9时出现出现如下错误: Checking if your kit is complete- Looks good Warning: prerequisite DBD::mysql 3 not found. Writing Makefile for percona-toolkit 根据建议应该是perl使用的mysql模块未安装所致 解决方案如下: 1.若是centos机器又是有安装yum的话,那么直接yum install perl-DBD-MySQ

PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法_php技巧

本文实例讲述了PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法.分享给大家供大家参考.具体分析如下: 今天在一朋友服务器测试一个网站时发现我在测试phpinfo时碰到PHP Warning:phpinfo() has been disabled for security reasons 提示了,按话的意思我总结了解决办法,下面我们一起来看看吧. 在运行phpinfo时碰到提示如下:PHP Warning: phpinfo() has been d

php下foreach提示Warning:Invalid argument supplied for foreach()的解决方法_php技巧

本文实例讲述了php下foreach()错误提示Warning: Invalid argument supplied for foreach() 的解决方法.分享给大家供大家参考.具体实现方法如下: 一.问题: php下foreach()错误提示Warning: Invalid argument supplied for foreach() 错误提示:Warning: Invalid argument supplied for foreach() in E:wampwwwmyshopcart.p

php提示Warning:mysql_fetch_array() expects的解决方法_php技巧

本文实例讲述了php提示Warning mysql_fetch_array() expects的解决方法,分享给大家供大家参考.具体分析如下: 在mysql数据库连接时碰到Warning: mysql_fetch_array() expects ...错误提示,根据我的经验这个是sql返回的query为空了,我们没有加己判断直接使用了. mysql_fetch_array()函数导致的,下面我们一起来看问题解决方案,我的代码如下: 复制代码 代码如下: include("conn.php&quo

c语言-为什么下述程序在VC中可编译,在gcc中提示warning

问题描述 为什么下述程序在VC中可编译,在gcc中提示warning #include int main () { int a=3,b=4,c=5; float x=1.2,y=2.4,z=-3.6; long u=51274,n=128765; char c1='a'; char c2='b'; printf("a=%2d b=%2d c=%2dn",a,b,c); printf("x=%8f,y=%8f,z=%9fn",x,y,z); printf("

MySQL无法重启提示Warning: World-writable config file

今天帮朋友维护服务器,在关闭数据库的命令发现mysql关不了,提示Warning: World-writable config file '/etc/my.cnf' is ignored ,大概意思是权限全局可写,任何一个用户都可以写.mysql担心这种文件被其他用户恶意修改,所以忽略掉这个配置文件.这样mysql无法关闭. 下面看下整个过程 重启MySQL  代码如下 复制代码 [root@ttlsa ~] # service mysqld stop Warning: World-writab

解决ecshop页面提示Warning: Division by zero in方法

ecshop部分模板使用时,会出现"Warning: Division by zero in-lib_goods.php"错误,原因在错误页面指定行数存在参数为0,计算错误. 解决方法:(适合小米模板.WAP模板.京东模板等) 打开includes/ lbi_goods.php文件 找到代码:$goods[$idx]['zhekou'] = round(($row['promote_price']/$row['market_price'])*100); 这个的上面加上代码: if($r

让你提前认识软件开发(51):VC++集成开发环境中Linux下Pclint工程的配置方法及常见错误修改

第3部分 软件研发工作总结 VC++集成开发环境中Linux下Pclint工程的配置方法及常见错误修改   [文章摘要]         Pclint是一种C/C++软件代码静态分析工具.它是一种更加严格的编译器,能够发现普通编译器所不能发现的代码中的很多问题,因此被广泛应用于软件开发项目中.        本文介绍了如何在VC++集成开发环境中配置Linux下的Pclint工程,给出了C语言中pclint规则A检查的常见错误,并描述了对应的修改办法.   [关键词]          VC++

linux下用core和gdb查询出现&quot;段错误&quot;的地方【转】

转自:http://blog.chinaunix.net/uid-30091091-id-5754288.html 原文地址:linux下用core和gdb查询出现"段错误"的地方 作者:草根老师   有些时候我们在一段C代码的时候,由于对一个非法内存进行了操作,在程序运行的过程中,出现了"段错误".   呵呵,这种问题我想很多人会经常遇到.遇到这种问题是非常无语的,只是提示了"段错误",接着什么都没 有,如果我们一味的去看代码找太疼苦了,因为我