PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/application/library/Captcha.php

https://gitlab.com/lcp0578/yaf-1
PHP | 113 lines | 78 code | 15 blank | 20 comment | 6 complexity | 94abe4805ca5abca816935017223f773 MD5 | raw file
  1. <?php
  2. /**
  3. * chenzhidong
  4. * 2013-6-1
  5. */
  6. class Captcha {
  7. private static function words($len) {
  8. $charecters = "123456789abcdefghijklmnpqrstuvwxyz";
  9. $words = "";
  10. $max = strlen($charecters) - 1;
  11. for ($i = 0; $i < $len; $i++)
  12. {
  13. $words .= $charecters[rand(0, $max)];
  14. }
  15. return $words;
  16. }
  17. /**
  18. * 生成验证码字符串,写入SESSION,将字符串图片返回给浏览器
  19. *
  20. * @param $len
  21. * @param int $width
  22. * @param int $height
  23. * @param int $font_size
  24. */
  25. public static function generate($len, $width = 108, $height = 30, $font_size = 18) {
  26. $sizes = array('18' => array('width' => 25, 'height' => 25));
  27. $words = self::words($len);
  28. session_start();
  29. $session_key = 'captcha';
  30. $_SESSION[$session_key] = strtolower($words);
  31. $image = ImageManager::createWhiteImage($width, $height);
  32. $font_config = array('spacing' => -17, 'font' => '5.ttf');
  33. $font_path = dirname(__FILE__) . '/font/' . $font_config['font'];
  34. $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));
  35. $rand = 0;
  36. $w = $sizes[$font_size]['width'] * $len;
  37. $h = $sizes[$font_size]['height'];
  38. $x = round(($width - $w) / 2);
  39. $y = round(($height + $h) / 2) - 6;
  40. $coors = imagettftext($image, $font_size, $rand, $x, $y, $color, $font_path, $words);
  41. if ($coors)
  42. {
  43. header("Cache-Control: no-cache, must-revalidate");
  44. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  45. header("Pragma: no-cache");
  46. header("Cache-control: private");
  47. header('Content-Type: image/png');
  48. imagepng($image);
  49. imagedestroy($image);
  50. }
  51. exit;
  52. }
  53. public static function simple($len, $width = 48, $height = 22) {
  54. $words = self::words($len);
  55. session_start();
  56. $session_key = 'captcha';
  57. $_SESSION[$session_key] = strtolower($words);
  58. $width = ($len * 10 + 10) > $width ? $len * 10 + 10 : $width;
  59. $canvas = imagecreatetruecolor($width, $height);
  60. $r = Array(225, 255, 255, 223);
  61. $g = Array(225, 236, 237, 255);
  62. $b = Array(225, 236, 166, 125);
  63. $key = mt_rand(0, 3);
  64. $back = imagecolorallocate($canvas, $r[$key], $g[$key], $b[$key]);
  65. $border = imagecolorallocate($canvas, 100, 100, 100);
  66. imagefilledrectangle($canvas, 0, 0, $width - 1, $height - 1, $back);
  67. imagerectangle($canvas, 0, 0, $width - 1, $height - 1, $border);
  68. $string = imagecolorallocate($canvas, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
  69. for ($i = 0; $i < 10; $i++)
  70. imagearc($canvas, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 200), mt_rand(20, 200), 55, 44, $string);
  71. for ($i = 0; $i < 25; $i++)
  72. imagesetpixel($canvas, mt_rand(0, $width), mt_rand(0, $height), $string);
  73. for ($i = 0; $i < $len; $i++)
  74. imagestring($canvas, 5, $i * 10 + 5, mt_rand(1, 8), $words{$i}, $string);
  75. if ($canvas)
  76. {
  77. header("Cache-Control: no-cache, must-revalidate");
  78. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  79. header("Pragma: no-cache");
  80. header("Cache-control: private");
  81. header('Content-Type: image/png');
  82. imagepng($canvas);
  83. imagedestroy($canvas);
  84. }
  85. exit;
  86. }
  87. /**
  88. * 验证是否是合法的验证码
  89. *
  90. * @param $captcha
  91. * @param int $size
  92. *
  93. * @return int
  94. */
  95. public static function isCaptcha($captcha, $size = 4) {
  96. return (bool)preg_match('/^[123456789abcdefghijklmnpqrstuvwxyz]{' . $size . '}$/ui', $captcha);
  97. }
  98. }