vaild.init.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php class vaildInit
  2. {
  3. var $Width = 60; //图片宽
  4. var $Height = 30; //图片高
  5. var $Length = 4; //验证码位数
  6. var $BgColor = "#FFFFFF"; //背景色
  7. var $TFonts = array("font.ttf");
  8. var $TFontSize=array(17,20); //字体大小范围
  9. var $TFontAngle=array(-20,20); //旋转角度
  10. var $Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //验证码范围(字母数字)
  11. var $Code = array(); //验证码
  12. var $Image = ""; //图形对象
  13. var $FontColors=array('#f36161','#6bc146','#5368bd'); //字体颜色,红绿蓝
  14. var $TPadden = 0.75;///字符间距
  15. var $Txbase = 5;///x轴两边距离
  16. var $Tybase =5 ;///y轴两边距离
  17. var $TLine =true; ///画干扰线
  18. public function RandRSI() ///生成验证码
  19. {
  20. $this->TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]);
  21. $this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]);
  22. $arr=array();
  23. $Chars=$this->Chars;
  24. $TFontAngle=$this->TFontAngle;
  25. $TFontSize=$this->TFontSize;
  26. $FontColors=$this->FontColors;
  27. $code="";
  28. $font=dirname(__FILE__)."/font/".$this->TFonts[0];
  29. $charlen=strlen($Chars)-1;
  30. $anglelen=count($TFontAngle)-1; // 角度范围
  31. $fontsizelen=count($TFontSize)-1; // 角度范围
  32. $fontcolorlen=count($FontColors)-1; // 角度范围
  33. for($i=0;$i<$this->Length;$i++) ///得到字符与颜色
  34. {
  35. $char=$Chars[rand(0,$charlen)]; ///得到字符
  36. $angle=$TFontAngle[rand(0,$anglelen)]; ///旋转角度
  37. $fontsize=$TFontSize[rand(0,$fontsizelen)]; ///字体大小
  38. $fontcolor=$FontColors[rand(0,$fontcolorlen)]; ///字体大小
  39. $bound=$this->_calculateTextBox($fontsize,$angle,$font,$char); ///得到范围
  40. $arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound); ///得到矩形框
  41. $code.=$char;
  42. }
  43. $this->Code=$arr; //验证码
  44. return $code;
  45. }
  46. public function Draw() ///画图
  47. {
  48. if(empty($this->Code)) $this->RandRSI();
  49. $codes=$this->Code; ///用户验证码
  50. $wh=$this->_getImageWH($codes);
  51. $width=$wh[0];
  52. $height=$wh[1]; ///高度
  53. $this->Width=$width;
  54. $this->Height=$height;
  55. $this->Image = imageCreate( $width, $height );
  56. $image=$this->Image;
  57. $back = $this->_getColor2($this->_getColor( $this->BgColor)); ///背景颜色
  58. imageFilledRectangle($image, 0, 0, $width, $height, $back); ///填充背景
  59. $TPadden=$this->TPadden;
  60. $basex=$this->Txbase;
  61. $color=null;
  62. foreach ($codes as $v) ///逐个画字符
  63. {
  64. $bound=$v[5];
  65. $color=$this->_getColor2($this->_getColor($v[2]));
  66. imagettftext($image, $v[0], $v[1], $basex, $bound['height'],$color , $v[4], $v[3]);
  67. $basex=$basex+$bound['width']*$TPadden-$bound['left'];///计算下一个左边距
  68. }
  69. $this->TLine?$this->_wirteSinLine($color,$basex):null; ///画干扰线
  70. header("Content-type: image/png");
  71. imagepng( $image);
  72. imagedestroy($image);
  73. }
  74. /**
  75. *通过字体角度得到字体矩形宽度*
  76. *
  77. * @param int $font_size 字体尺寸
  78. * @param float $font_angle 旋转角度
  79. * @param string $font_file 字体文件路径
  80. * @param string $text 写入字符
  81. * @return array 返回长宽高
  82. */
  83. private function _calculateTextBox($font_size, $font_angle, $font_file, $text) {
  84. $box = imagettfbbox($font_size, $font_angle, $font_file, $text);
  85. $min_x = min(array($box[0], $box[2], $box[4], $box[6]));
  86. $max_x = max(array($box[0], $box[2], $box[4], $box[6]));
  87. $min_y = min(array($box[1], $box[3], $box[5], $box[7]));
  88. $max_y = max(array($box[1], $box[3], $box[5], $box[7]));
  89. return array(
  90. 'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),
  91. 'top' => abs($min_y),
  92. 'width' => $max_x - $min_x,
  93. 'height' => $max_y - $min_y,
  94. 'box' => $box
  95. );
  96. }
  97. private function _getColor( $color ) //#ffffff
  98. {
  99. return array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6]));
  100. }
  101. private function _getColor2( $color ) //#ffffff
  102. {
  103. return imagecolorallocate ($this->Image, $color[0], $color[1], $color[2]);
  104. }
  105. private function _getImageWH($data)
  106. {
  107. $TPadden=$this->TPadden;
  108. $w=$this->Txbase;
  109. $h=0;
  110. foreach ($data as $v)
  111. {
  112. $w=$w+$v[5]['width']*$TPadden-$v[5]['left'];
  113. $h=$h>$v[5]['height']?$h:$v[5]['height'];
  114. }
  115. return array(max($w,$this->Width),max($h,$this->Height));
  116. }
  117. //画正弦干扰线
  118. private function _wirteSinLine($color,$w)
  119. {
  120. $img=$this->Image;
  121. $h=$this->Height;
  122. $h1=rand(-5,5);
  123. $h2=rand(-1,1);
  124. $w2=rand(10,15);
  125. $h3=rand(4,6);
  126. for($i=-$w/2;$i<$w/2;$i=$i+0.1)
  127. {
  128. $y=$h/$h3*sin($i/$w2)+$h/2+$h1;
  129. imagesetpixel($img,$i+$w/2,$y,$color);
  130. $h2!=0?imagesetpixel($img,$i+$w/2,$y+$h2,$color):null;
  131. }
  132. }
  133. }?>