在开发时出现uft-8页面中文验证码乱码了,在baidu搜索发现了原因,下面有解决办法有需要的朋友可以参考一下,代码如下:
- <?
- 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);
- }
-
- 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');
-
-
-
- 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);
|