使用imagettftext生成中文验证码图片时 出现Warning: imagettftext(): Could not find/open font in错误

$fontfile='./ttf/simhei.ttf';改成 $fontfile='D:\phpstudy_pro\WWW\cheshi\ttf\simhei.ttf';

把字体文件改为绝对路径

或者这样改也可以

$fontfile='./ttf/simhei.ttf';    //字体路径
$fontfile=realpath($fontfile);

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
//第一步:生成随机字符串
$codeSet='们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来';
//多字节处理   需要开启extension=mbstring
$max=mb_strlen($codeSet)-1;//中文字符的最大索引号
$code="";
for($i=1;$i<=4;$i++){
   $start=rand(0,$max);
   $code.=mb_substr($codeSet,$start,1);
}

//echo $code;
//第二步:创建画布
$img=imagecreate(150, 40);
imagecolorallocate($img, 255, 0, 0);
//第三步:将字符串写到画布上

//3.1  指定字符串的参数
$color=imagecolorallocate($img,255,255,255);
$size=20;	//字号
$angle=0;	//角度
$fontfile='./ttf/simhei.ttf';	//字体路径
$fontfile=realpath($fontfile);


//3.2 测定字符串的范围
$info=imagettfbbox($size,$angle,$fontfile,$code);
$code_w=$info[4]-$info[6];	//字符串的宽度
$code_h=$info[1]-$info[7];	//字符串的高度

$x=(imagesx($img)-$code_w)/2;	//起始点的$x
$y=(imagesy($img)+$code_h)/2;	//起始点的$y
//3.3  将中文字符串写到画布上
imagettftext($img,$size,$angle,$x,$y,$color,$fontfile,$code);	//将文字写到画布上


//显示验证码
header('content-type:image/gif');
imagegif($img);


?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值