ValidateCode.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\library;
  3. use think\Session;
  4. class ValidateCode {
  5. /**
  6. * 验证码的session的下标
  7. *
  8. * @var string
  9. */
  10. //public static $seKey = 'sid.sek ey.ylans.cn';
  11. public static $seKey = 'mngid';
  12. public static $expire = 3000; // 验证码过期时间(s)
  13. /**
  14. * 验证码中使用的字符,01IO容易混淆,建议不用
  15. *
  16. * @var string
  17. */
  18. public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY';
  19. public static $fontSize = 25; // 验证码字体大小(px)
  20. public static $useCurve = true; // 是否画混淆曲线
  21. public static $useNoise = true; // 是否添加杂点
  22. public static $imageH = 0; // 验证码图片宽
  23. public static $imageL = 0; // 验证码图片长
  24. public static $length = 4; // 验证码位数
  25. public static $bg = array(243, 251, 254); // 背景
  26. protected static $_image = null; // 验证码图片实例
  27. protected static $_color = null; // 验证码字体颜色
  28. protected static $_fonts = ["BAUHS93.TTF","BERNHC.TTF","PLAYBILL.TTF","POORICH.TTF","REFSAN.TTF"]; // 验证码字体颜色
  29. /**
  30. * 输出验证码并把验证码的值保存的session中
  31. * 验证码保存到session的格式为: $_SESSION[self::$seKey] = array('code' => '验证码值', 'time' => '验证码创建时间');
  32. */
  33. public static function entry() {
  34. // 图片宽(px)
  35. self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize * 1.5;
  36. // 图片高(px)
  37. self::$imageH || self::$imageH = self::$fontSize * 2;
  38. // 建立一幅 self::$imageL x self::$imageH 的图像
  39. self::$_image = imagecreate(self::$imageL, self::$imageH);
  40. // 设置背景
  41. imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]);
  42. // 验证码字体随机颜色
  43. self::$_color = imagecolorallocate(self::$_image, mt_rand(1, 120), mt_rand(1, 120), mt_rand(1, 120));
  44. // 验证码使用随机字体
  45. //$ttf = dirname(__FILE__) . '/ttfs/' . mt_rand(1, 20) . '.ttf'; 4
  46. $ttf = dirname(__FILE__) . '/fonts/BAUHS93.TTF';
  47. if (self::$useNoise) {
  48. // 绘杂点
  49. // self::_writeNoise();
  50. }
  51. if (self::$useCurve) {
  52. // 绘干扰线
  53. self::_writeCurve();
  54. }
  55. // 绘验证码
  56. $code = array(); // 验证码
  57. $codeNX = 0; // 验证码第N个字符的左边距
  58. for ($i = 0; $i < self::$length; $i++) {
  59. $code[$i] = self::$codeSet[mt_rand(0, 27)];
  60. $codeNX += mt_rand(self::$fontSize * 1.2, self::$fontSize * 1.6);
  61. // 写一个验证码字符
  62. imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize * 1.5, self::$_color, self::getFont(), $code[$i]);
  63. }
  64. // 保存验证码
  65. isset($_SESSION) || session_start();
  66. //$_SESSION[self::$seKey]['code'] = join('', $code); // 把校验码保存到session
  67. Session::set(self::$seKey,join('', $code));
  68. //$_SESSION[self::$seKey]['time'] = time(); // 验证码创建时间
  69. header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
  70. header('Cache-Control: post-check=0, pre-check=0', false);
  71. header('Pragma: no-cache');
  72. header("content-type: image/png");
  73. // 输出图像
  74. imagepng(self::$_image);
  75. imagedestroy(self::$_image);
  76. }
  77. /**
  78. * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
  79. *
  80. * 高中的数学公式咋都忘了涅,写出来
  81. * 正弦型函数解析式:y=Asin(ωx+φ)+b
  82. * 各常数值对函数图像的影响:
  83. * A:决定峰值(即纵向拉伸压缩的倍数)
  84. * b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
  85. * φ:决定波形与X轴位置关系或横向移动距离(左加右减)
  86. * ω:决定周期(最小正周期T=2π/∣ω∣)
  87. *
  88. */
  89. protected static function _writeCurve() {
  90. $A = mt_rand(1, self::$imageH / 2); // 振幅
  91. $b = mt_rand(-self::$imageH / 4, self::$imageH / 4); // Y轴方向偏移量
  92. $f = mt_rand(-self::$imageH / 4, self::$imageH / 4); // X轴方向偏移量
  93. $T = mt_rand(self::$imageH * 1.5, self::$imageL * 2); // 周期
  94. $w = (2 * M_PI) / $T;
  95. $px1 = 0; // 曲线横坐标起始位置
  96. $px2 = mt_rand(self::$imageL / 2, self::$imageL * 0.667); // 曲线横坐标结束位置
  97. for ($px = $px1; $px <= $px2; $px = $px + 0.9) {
  98. if ($w != 0) {
  99. $py = $A * sin($w * $px + $f) + $b + self::$imageH / 2; // y = Asin(ωx+φ) + b
  100. $i = (int) ((self::$fontSize - 6) / 4);
  101. while ($i > 0) {
  102. imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里画像素点比imagettftext和imagestring性能要好很多
  103. $i--;
  104. }
  105. }
  106. }
  107. $A = mt_rand(1, self::$imageH / 2); // 振幅
  108. $f = mt_rand(-self::$imageH / 4, self::$imageH / 4); // X轴方向偏移量
  109. $T = mt_rand(self::$imageH * 1.5, self::$imageL * 2); // 周期
  110. $w = (2 * M_PI) / $T;
  111. $b = $py - $A * sin($w * $px + $f) - self::$imageH / 2;
  112. $px1 = $px2;
  113. $px2 = self::$imageL;
  114. for ($px = $px1; $px <= $px2; $px = $px + 0.9) {
  115. if ($w != 0) {
  116. $py = $A * sin($w * $px + $f) + $b + self::$imageH / 2; // y = Asin(ωx+φ) + b
  117. $i = (int) ((self::$fontSize - 8) / 4);
  118. while ($i > 0) {
  119. imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
  120. $i--;
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * 画杂点
  127. * 往图片上写不同颜色的字母或数字
  128. */
  129. protected static function _writeNoise() {
  130. for ($i = 0; $i < 10; $i++) {
  131. //杂点颜色
  132. $noiseColor = imagecolorallocate(
  133. self::$_image, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225)
  134. );
  135. for ($j = 0; $j < 5; $j++) {
  136. // 绘杂点
  137. imagestring(
  138. self::$_image, 5, mt_rand(-10, self::$imageL), mt_rand(-10, self::$imageH), self::$codeSet[mt_rand(0, 27)], // 杂点文本为随机的字母或数字
  139. $noiseColor
  140. );
  141. }
  142. }
  143. }
  144. /**
  145. * 验证验证码是否正确
  146. *
  147. * @param string $code 用户验证码
  148. * @param bool 用户验证码是否正确
  149. */
  150. public static function check($code) {
  151. isset($_SESSION) || session_start();
  152. // 验证码不能为空
  153. if (empty($code) || empty($_SESSION[self::$seKey])) {
  154. //echo $_SESSION[self::$seKey]['code'].'1';
  155. return false;
  156. }
  157. // session 过期
  158. if (time() - $_SESSION[self::$seKey]['time'] > self::$expire) {
  159. unset($_SESSION[self::$seKey]);
  160. //echo $_SESSION[self::$seKey]['code'].'2';
  161. return false;
  162. //return 0;
  163. }
  164. // if($code == $_SESSION[self::$seKey]['code']) {
  165. if (strtoupper($code) == $_SESSION[self::$seKey]['code']) { //不区分大小写比较
  166. //echo $_SESSION[self::$seKey]['code'].'3';
  167. return true;
  168. }
  169. //echo $_SESSION[self::$seKey]['code'].'4';
  170. return false;
  171. }
  172. /**
  173. * 获取一个字体
  174. * @return type
  175. */
  176. private static function getFont(){
  177. $index=mt_rand(0,count(self::$_fonts)-1);
  178. $fontTmp=self::$_fonts[$index];
  179. return dirname(__FILE__) . '/fonts/' . $fontTmp;
  180. }
  181. }
  182. // useage
  183. /*
  184. ValidateCode::$useNoise = false; // 要更安全的话改成true
  185. ValidateCode::$useCurve = true;
  186. ValidateCode::entry();
  187. */
  188. /*
  189. // 验证验证码
  190. if (!ValidateCode::check(@$_POST['secode'])) {
  191. print 'error secode';
  192. }
  193. */