为了账号安全,请及时绑定邮箱和手机立即绑定

我的汉字验证码,可以输出,但是汉字显示是乱码,以下是我的图和代码,我的编码格式utf-8,utf8,gb2312都试了

http://img1.sycdn.imooc.com//574923370001524807400439.jpg

<?php
      session_start();

      $image=imagecreatetruecolor(200, 60);
      $bgcolor=imagecolorallocate($image, 255, 255, 255);
      imagefill($image, 0, 0, $bgcolor);
      
      $fontface='FZYTK.TTF'; //字体一定要命名为英文
      
      $str = "的一是在了不和有大这主中人上为们地个用工时要动国产以
           走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话";

      //$strdb=array('慕' , '课' ,'网' ,'赞' );
      $strdb=str_split($str,3);
      //header('content-type:text/html; charset=gb2312');
      //var_dump($strdb);
      //die();
     $captch_code='';
     for($i=0;$i<4;$i++)//写汉字       
     {
         $fontcolor=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));//字体颜色
        
         $index=rand(0,count($strdb));
         $cn=$strdb[$index];
         $captch_code.=$cn;

         imagettftext($image, mt_rand(20,24), mt_rand(-60,60),(40*$i+20), mt_rand(30,35),  $fontcolor, $fontface, $cn);
         //imagettftext(image, size, angle, x, y, color, fontfile, text)
       }
      

      $_SESSION['authcode']=$captch_code;

      for ($i=0; $i < 200; $i++) //干扰点
      {
          $pointcolor=imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));
          imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
      }
      
       for ($i=0; $i < 3; $i++) //干扰线
      {
          $linecolor=imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220));
          imageline($image, rand(1,199), rand(1,59),rand(1,199), rand(1,59), $linecolor);
      }

      header('content-type: image/png; charset=gb2312');
      imagepng($image);

      imagedestroy($image);
      
?>

正在回答

5 回答

将 .php文件重新另存为 UTF-8 无BOM 的编码格式,让后就行了

0 回复 有任何疑惑可以回复我~

UTF-8 档首无 BOM

0 回复 有任何疑惑可以回复我~

解决没?我也出现了这个问题

0 回复 有任何疑惑可以回复我~

改成utf-8 无bom 就不会乱码了

0 回复 有任何疑惑可以回复我~

应该是你的验证那个页面的编码格式没有设置,或者设置的不是和utf-8的

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

我的汉字验证码,可以输出,但是汉字显示是乱码,以下是我的图和代码,我的编码格式utf-8,utf8,gb2312都试了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信