captcha.init.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. defined('IS_INITPHP') || die('Access Denied!');
  3. /**
  4. * 扩展类库-验证码
  5. */
  6. class captchaInit {
  7. // 验证码位数
  8. private $mCheckCodeNum = 4;
  9. // 产生的验证码
  10. private $mCheckCode = '';
  11. // 验证码的图片
  12. private $mCheckImage = '';
  13. // 干扰像素
  14. private $mDisturbColor = '';
  15. // 验证码的图片宽度
  16. private $mCheckImageWidth = '60';
  17. // 验证码的图片宽度
  18. private $mCheckImageHeight = '20';
  19. /**
  20. * @brief 输出头
  21. */
  22. private function OutFileHeader() {
  23. header( "Content-type: image/png" );
  24. }
  25. /**
  26. * * @brief 产生验证码
  27. */
  28. public function CreateCheckCode() {
  29. $this->mCheckCode = $this->mKey( $this->mCheckCodeNum, 'ALNUM' );
  30. return $this->mCheckCode;
  31. }
  32. private function mKey($len = 4, $type = 'ALNUM') {
  33. // Register the lower case alphabet array
  34. $alpha = array(
  35. 'a',
  36. 'b',
  37. 'c',
  38. 'd',
  39. 'e',
  40. 'f',
  41. 'g',
  42. 'h',
  43. 'i',
  44. 'j',
  45. 'k',
  46. 'l',
  47. 'm',
  48. 'n',
  49. 'o',
  50. 'p',
  51. 'q',
  52. 'r',
  53. 's',
  54. 't',
  55. 'u',
  56. 'v',
  57. 'w',
  58. 'x',
  59. 'y',
  60. 'z'
  61. );
  62. // Register the upper case alphabet array
  63. $ALPHA = array(
  64. 'A',
  65. 'B',
  66. 'C',
  67. 'D',
  68. 'E',
  69. 'F',
  70. 'G',
  71. 'H',
  72. 'J',
  73. 'K',
  74. 'L',
  75. 'M',
  76. 'N',
  77. 'P',
  78. 'Q',
  79. 'R',
  80. 'S',
  81. 'T',
  82. 'U',
  83. 'V',
  84. 'W',
  85. 'X',
  86. 'Y',
  87. 'Z'
  88. );
  89. // Register the numeric array
  90. $num = array(
  91. '0',
  92. '1',
  93. '2',
  94. '3',
  95. '4',
  96. '5',
  97. '6',
  98. '7',
  99. '8',
  100. '9'
  101. );
  102. // Initialize the keyVals array for use in the for loop
  103. $keyVals = array();
  104. // Initialize the key array to register each char
  105. $key = array();
  106. // Loop through the choices and register
  107. // The choice to keyVals array
  108. switch($type) {
  109. case 'lower' :
  110. $keyVals = $alpha;
  111. break;
  112. case 'upper' :
  113. $keyVals = $ALPHA;
  114. break;
  115. case 'numeric' :
  116. $keyVals = $num;
  117. break;
  118. case 'ALPHA' :
  119. $keyVals = array_merge( $alpha, $ALPHA );
  120. break;
  121. case 'ALNUM' :
  122. $keyVals = array_merge( $ALPHA, $num );
  123. break;
  124. }
  125. // Loop as many times as specified
  126. // Register each value to the key array
  127. for($i = 0; $i <= $len - 1; $i ++) {
  128. $r = rand( 0, count( $keyVals ) - 1 );
  129. $key [$i] = $keyVals [$r];
  130. }
  131. // Glue the key array into a string and return it
  132. return join( "", $key );
  133. }
  134. /**
  135. * @brief 产生验证码图片
  136. */
  137. private function CreateImage() {
  138. $this->mCheckImage = @imagecreate( $this->mCheckImageWidth, $this->mCheckImageHeight );
  139. imagecolorallocate( $this->mCheckImage, 200, 200, 200 );
  140. return $this->mCheckImage;
  141. }
  142. /**
  143. * @brief 设置图片的干扰像素
  144. */
  145. private function SetDisturbColor() {
  146. for($i = 0; $i <= 128; $i ++) {
  147. $this->mDisturbColor = imagecolorallocate( $this->mCheckImage, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) );
  148. imagesetpixel( $this->mCheckImage, rand( 2, 128 ), rand( 2, 38 ), $this->mDisturbColor );
  149. }
  150. }
  151. /**
  152. *
  153. *
  154. * @brief 设置验证码图片的大小
  155. *
  156. * @param $width 宽
  157. *
  158. * @param $height 高
  159. *
  160. */
  161. public function SetCheckImageWH($width, $height) {
  162. if($width == '' || $height == '')
  163. return false;
  164. $this->mCheckImageWidth = $width;
  165. $this->mCheckImageHeight = $height;
  166. return true;
  167. }
  168. /**
  169. * @brief 在验证码图片上逐个画上验证码
  170. */
  171. private function WriteCheckCodeToImage() {
  172. for($i = 0; $i <= $this->mCheckCodeNum; $i ++) {
  173. $bg_color = imagecolorallocate( $this->mCheckImage, rand( 0, 255 ), rand( 0, 128 ), rand( 0, 255 ) );
  174. $x = floor( $this->mCheckImageWidth / $this->mCheckCodeNum ) * $i;
  175. $y = rand( 0, $this->mCheckImageHeight - 15 );
  176. imagechar( $this->mCheckImage, 5, $x, $y, $this->mCheckCode [$i], $bg_color );
  177. }
  178. }
  179. /**
  180. * @brief 输出验证码图片
  181. */
  182. public function OutCheckImage() {
  183. $this->OutFileHeader();
  184. $this->CreateImage();
  185. $this->SetDisturbColor();
  186. $this->WriteCheckCodeToImage();
  187. imagepng( $this->mCheckImage );
  188. imagedestroy( $this->mCheckImage );
  189. }
  190. }