当前位置:Gxlcms > PHP教程 > 关于中文验证码的问题,不解

关于中文验证码的问题,不解

时间:2021-07-01 10:21:17 帮助过:6人阅读

闲来无事,写了个带中文一、二、三。。。的加减验证码,可以老是中文出错,找了要用iconv函数,但是我的页面编码本身就是UTF-8的,还请高手帮帮忙,闲着无聊的欢迎灌灌水。
  1. <!--?php session_start();$_SESSION['code']= 0;header("Content-type: image/png"); $im = @imagecreatetruecolor(70, 27) or die("建立图像失败"); //创建图片$background_color = imagecolorallocate($im, 250, 250, 250); //图片添加背景imagefill($im,0,0,$background_color);$border_color = imagecolorallocate($im,0,0,0); //边框色imagerectangle($im,0,0,69,26,$border_color);//噪音线for($i = 0;$i < 7;$i++){
  2. $x1 = rand(3,25);
  3. $y1 = rand(2,26);//开始位置
  4. $x2 = rand(45,68);//结束位置
  5. $y2 = rand(2,26);
  6. $line_color = imagecolorallocate($im,rand(190,255),rand(190,255),rand(190,255)); //噪音线颜色
  7. imageline($im,$x1,$y1,$x2,$y2,$line_color); }//显示的文字$font_size = 18;$first_num = 0;//图片中显示的数字,包含中文$second_num = 0;$first_value = 0;//后台预算的数字$second_value = 0;$hanzi_num = array("一","二","三","四","五","六","七","八","九","十");//$str_HanZiOrNot = rand(0,1);//是否是汉字的判断$result = 0;$arrCapcher = array("+","-");$capter = $arrCapcher[rand(0,1)];switch ($capter){
  8. case "+":
  9. //产生第一个数字
  10. if(rand(0,1) == 0){// ==0表示纯数字
  11. $first_num = rand(2,10);
  12. $first_value = $first_num;
  13. }
  14. else{ //汉字
  15. $index_ = rand(0,10);
  16. $first_num = $hanzi_num[$index_];
  17. $first_value = $index_ + 1;
  18. }
  19. //产生第二个数字
  20. if(rand(0,1) == 0){// ==0纯数字
  21. $second_num = rand(0,10);
  22. $second_value = $second_value;
  23. }
  24. else{
  25. $index_ = rand(0,10);
  26. $second_num = $hanzi_num[$index_];
  27. $second_value = $index_ + 1;
  28. }
  29. $result = $first_value + $second_value;
  30. break;
  31. case "-":
  32. //产生第一个数字
  33. if(rand(0,1) == 0){// ==0表示纯数字
  34. $first_num = rand(2,10);
  35. $first_value = $first_num;
  36. }
  37. else{ //汉字
  38. $index_ = rand(0,10);
  39. $first_num = $hanzi_num[$index_];
  40. $first_value = $index_ + 1;
  41. }
  42. //产生第二个数字
  43. if(rand(0,1) == 0){// ==0纯数字
  44. $second_num = rand(0,$first_value);
  45. $second_value = $second_value;
  46. }
  47. else{
  48. $index_ = rand(0,$first_value-1);
  49. $second_num = $hanzi_num[$index_];
  50. $second_value = $index_ + 1;
  51. }
  52. $result = $first_value - $second_value;
  53. break;}$x = rand(2,5);$arr_X_Y = array(array($x,rand(1,3)),array($x+$font_size,rand(1,4)),array($x+2*$font_size,rand(1,3)),array($x+3*$font_size,rand(1,4)));$str_char = array($first_num,$capter,$second_num,"=");for($i = 0;$i< 4;$i++){
  54. $text_color = imagecolorallocate($im,rand(50,120),rand(50,120),rand(50,120));
  55. imagechar($im,$font_size,$arr_X_Y[$i][0],$arr_X_Y[$i][1],$str_char[$i],$text_color); }$_SESSION['code'] = $result;imagepng($im); imagedestroy($im);?-->


回复讨论(解决方案)

大神们都睡觉了吗? 那我明天再来好了

imagechar 使用内置的点阵字体,不支持中文
你要用 magettftext 来完成

imagechar 使用内置的点阵字体,不支持中文
你要用 magettftext 来完成 膜拜

人气教程排行