php 图片中文验证码

  1. <img src="verify_image.php" alt="点此刷新验证码" name="verify_code" width="65" height="20" onclick="document.getElementById('verify_code').src='verify_image.php?'+Math.random();" />
  2. <?php
  3. session_start();
  4. $vi = new vCodeImage;
  5. $vi->SetImage(1,4,65,20,80,1);
  6. class vCodeImage{
  7. var $mode; //1:数字模式,2:字母模式,3:数字字母模式,其他:数字字母优化模式
  8. var $v_num; //验证码个数
  9. var $img_w; //验证码图像宽度
  10. var $img_h; //验证码图像高度
  11. var $int_pixel_num; //干扰像素个数
  12. var $int_line_num; //干扰线条数
  13. var $font_dir; //字体文件相对路径
  14. var $border; //图像边框
  15. var $borderColor; //图像边框颜色
  16. function SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir='../font',$255,200,85'){
  17. if(!isset($_SESSION['vCode'])){
  18. session_register('vCode');
  19. }
  20. $_SESSION['vCode']="";
  21. $this->mode = $made;
  22. $this->v_num = $v_num;
  23. $this->img_w = $img_w;
  24. $this->img_h = $img_h;
  25. $this->int_pixel_num = $int_pixel_num;
  26. $this->int_line_num = $int_line_num;
  27. $this->font_dir = $font_dir;
  28. $this->border = $border;
  29. $this->borderColor = $borderColor;
  30. $this->GenerateImage();
  31. }
  32. function GetChar($mode){
  33. if($mode == "1"){
  34. $ychar = "0,1,2,3,4,5,6,7,8,9";
  35. }
  36. else if($mode == "2"){
  37. $ychar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  38. }
  39. else if($mode == "3"){
  40. $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  41. }
  42. else
  43. $ychar = "3,4,5,6,7,8,9,A,B,C,D,H,K,P,R,S,T,W,X,Y";
  44. return $ychar;
  45. }
  46. function RandColor($rs,$re,$gs,$ge,$bs,$be){
  47. $r = mt_rand($rs,$re);
  48. $g = mt_rand($gs,$ge);
  49. $b = mt_rand($bs,$be);
  50. return array($r,$g,$b);
  51. }
  52. function GenerateImage(){
  53. $im = imagecreate($this->img_w,$this->img_h);
  54. $black = imagecolorallocate($im, 0,0,0);
  55. $white = imagecolorallocate($im, 255,255,255);
  56. $bgcolor = imagecolorallocate($im, 250,250,250);
  57. imagefill($im,0,0,$bgcolor);
  58. $fonts = ScanDir($this->font_dir);
  59. $fmax = count($fonts) - 2;
  60. $ychar = $this->GetChar($this->mode);
  61. $list = explode(",",$ychar);
  62. $x = mt_rand(2,$this->img_w/($this->v_num+2));
  63. $cmax = count($list) - 1;
  64. $v_code = '';
  65. for($i=0;$i<$this->v_num;$i++) //验证码
  66. {
  67. $randnum = mt_rand(0,$cmax);
  68. $this_char = $list[$randnum];
  69. $v_code .= $this_char;
  70. $size = mt_rand(intval($this->img_w/5),intval($this->img_w/4));
  71. $angle = mt_rand(-20,20);
  72. $y = mt_rand(($size+2),($this->img_h-2));
  73. if($this->border)
  74. $y = mt_rand(($size+3),($this->img_h-3));
  75. $rand_color = $this->RandColor(0,200,0,100,0,250);
  76. $randcolor = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
  77. $fontrand = mt_rand(2, $fmax);
  78. $font = "$this->font_dir/".$fonts[$fontrand];
  79. imagettftext($im, $size, $angle, $x, $y, $randcolor, $font, $this_char);
  80. $x = $x + intval($this->img_w/($this->v_num+1));
  81. }
  82. for($i=0;$i<$this->int_pixel_num;$i++){//干扰像素
  83. $rand_color = $this->RandColor(50,250,0,250,50,250);
  84. $rand_color_pixel = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
  85. imagesetpixel($im, mt_rand()%$this->img_w, mt_rand()%$this->img_h, $rand_color_pixel);
  86. }
  87. for($i=0;$i<$this->int_line_num;$i++){ //干扰线
  88. $rand_color = $this->RandColor(0,250,0,250,0,250);
  89. $rand_color_line = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
  90. imageline($im, mt_rand(0,intval($this->img_w/3)), mt_rand(0,$this->img_h), mt_rand(intval($this->img_w - ($this->img_w/3)),$this->img_w), mt_rand(0,$this->img_h), $rand_color_line);
  91. }
  92. if($this->border) //画出边框
  93. {
  94. if(preg_match("/^\d{1,3},\d{1,3},\d{1,3}$/",$this->borderColor)){
  95. $borderColor = explode(',',$this->borderColor);
  96. }
  97. $border_color_line = imagecolorallocate($im,$borderColor[0],$borderColor[1],$borderColor[2]);
  98. imageline($im, 0, 0, $this->img_w, 0, $border_color_line); //上横
  99. imageline($im, 0, 0, 0, $this->img_h, $border_color_line); //左竖
  100. imageline($im, 0, $this->img_h-1, $this->img_w, $this->img_h-1, $border_color_line); //下横
  101. imageline($im, $this->img_w-1, 0, $this->img_w-1, $this->img_h, $border_color_line); //右竖
  102. }
  103. imageantialias($im,true); //抗锯齿
  104. $time = time();
  105. $_SESSION['vCode'] = $v_code."|".$time; //把验证码和生成时间负值给$_SESSION[vCode]
  106. //生成图像给浏览器
  107. if (function_exists("imagegif")) {
  108. header ("Content-type: image/gif");
  109. imagegif($im);
  110. }
  111. elseif (function_exists("imagepng")) {
  112. header ("Content-type: image/png");
  113. imagepng($im);
  114. }
  115. elseif (function_exists("imagejpeg")) {
  116. header ("Content-type: image/jpeg");
  117. imagejpeg($im, "", 80);
  118. }
  119. elseif (function_exists("imagewbmp")) {
  120. header ("Content-type: image/vnd.wap.wbmp");
  121. imagewbmp($im);
  122. }//开源代码phpfensi.com
  123. else
  124. die("No Image Support On This Server !");
  125. imagedestroy($im);
  126. }
  127. }
  128. ?>