php utf8页面验证码图片中文乱码
在开发时出现uft-8页面中文验证码乱码了,在baidu搜索发现了原因,下面有解决办法有需要的朋友可以参考一下,代码如下:
<?php session_start(); //生成随机数 for ($i = 0; $i < 4; $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 < 3; $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 < 200; $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);
本文地址:http://www.phprm.com/tuxiang/fs627.html
转载随意,但请附上文章地址:-)