Home  >  Article  >  Backend Development  >  php utf8页面验证码图片中文乱码_PHP教程

php utf8页面验证码图片中文乱码_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:55:04983browse

在开发时出现uft-8页面中文验证码乱码了,在baidu搜索发现了原因,下面有解决办法有需要的朋友可以参考一下。

 

 代码如下 复制代码


session_start();


//生成随机数
for($i=0;$i $rand.=dechex(rand(1,15));
}
$_SESSION['checkpic']=$rand;

$im=imagecreatetruecolor(100,30);//画板,新建一个真彩色图像

//设置颜色
$bg=imagecolorallocate($im,0,0,0);//红,绿,蓝 背景颜色
$te=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//字体颜色

//画线条
for($i=0;$i $te2=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,100),0,rand(0,100),30,$te2);//坐标(x1,y1)到坐标(x2,y2)
}

//画点
for($i=0;$i imagesetpixel($im,rand()%100,rand()%30,$te2);
}


//输出中文
$str=iconv("gbk","utf-8","新年快乐!");//确定要绘制的中文文字
imagettftext($im,12,3,20,20,$te,'msyhbd.ttf','中文en');

//把字符串写在图像左上角
//imagestring($im,5,rand(0,50),rand(0,15),$rand,$te);

//输出图像
header("Content-type:image/jpeg");//文件类型
imagejpeg($im);

 

?>

分析原因与解决办法

 代码如下 复制代码

$str=iconv("gbk","utf-8","新年快乐!");//确定要绘制的中文文字
imagettftext($im,12,3,20,20,$te,'msyhbd.ttf','中文en');

-->>
应该这样的

 代码如下 复制代码
$str=iconv("gbk","utf-8","新年快乐!");//确定要绘制的中文文字
imagettftext($im,12,3,20,20,$te,'msyhbd.ttf',$str);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632267.htmlTechArticle在开发时出现uft-8页面中文验证码乱码了,在baidu搜索发现了原因,下面有解决办法有需要的朋友可以参考一下。 代码如下 复制代码 ? sess...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn