生成验证码过程中出错,代码如下:
function getCheckCode()
{
header("content-type:image/gif");
$img = imagecreate($this->width, $this->height);
$bgcolor = imagecolorallocate($img, 255, 255, 255); //图像背景色
$strColor = imagecolorallocate($img, 255, 0, 0);//验证码字符颜色
$fontfile = "./msyh.ttf";//字体文件文件路径
//$fontfile = "E:\www\public\msyh.ttf";//字体文件文件路径
$size = 30; //字体大小
$angle = rand(-5, 5); //字体倾斜角度
//干扰元素的颜色
$color = imagecolorallocate($img, 100, 100, 100);
//调用糙点函数
$this->createPix($img, $color);
//调用干扰线
$this->createLine($img, $color);
//写入字符到图像
imagettftext($img, $size, $angle, 15, 50, $strColor, $fontfile, session("code"));
//输出图像
imagegif($img);
//销毁内存中的缓存
imagedestroy($img);
}
使用GD库生成验证码,出现imagettftext(): Could not find/open font,或者imagettftext(): Invalid font filename错误提示,最后将
$fontfile = “./msyh.ttf”;//字体文件文件路径
改成
$fontfile = “E:\www\public\msyh.ttf”;//字体文件文件路径
便可以显示验证码了,证明是字体路径的问题。当然你遇到的问题可能也是上面的提示,可能用此方法解决不了,通过此可以验证字体路径是否有问题。
解决方法:将路径改成正确的路径便可!
function getCheckCode()
{
header("content-type:image/gif");
$img = imagecreate($this->width, $this->height);
$bgcolor = imagecolorallocate($img, 255, 255, 255); //图像背景色
$strColor = imagecolorallocate($img, 255, 0, 0);//验证码字符颜色
$fontfile = "./msyh.ttf";//字体文件文件路径
//$fontfile = "E:\www\public\msyh.ttf";//字体文件文件路径
$size = 30; //字体大小
$angle = rand(-5, 5); //字体倾斜角度
//干扰元素的颜色
$color = imagecolorallocate($img, 100, 100, 100);
//调用糙点函数
$this->createPix($img, $color);
//调用干扰线
$this->createLine($img, $color);
//写入字符到图像
imagettftext($img, $size, $angle, 15, 50, $strColor, $fontfile, session("code"));
//输出图像
imagegif($img);
//销毁内存中的缓存
imagedestroy($img);
}
达维营-前端网