captcha.init.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 扩展类库-验证码(增强版)
  4. *
  5. * 功能特点:
  6. * 1. 生成并输出验证码图片,同时返回验证码,其存储方式以及校验由应用程序自行决定;
  7. * 2. 可自定义验证码图片大小,长度,字体及字体大小;
  8. *
  9. * @version 0.1.0
  10. * @author Anran <id0612@gmail.com>
  11. */
  12. if (!defined('IS_INITPHP')) exit('Access Denied!');
  13. /**
  14. * 扩展类库-验证码
  15. */
  16. class captchaInit {
  17. /** @var string $font 字体 */
  18. private $font = '';
  19. /** @var int $size 字体大小 */
  20. private $size = 18;
  21. /** @var int $width 宽度 */
  22. private $width = 120;
  23. /** @var int $height 高度 */
  24. private $height = 40;
  25. /** @var int $length 验证码长度 */
  26. private $length = 6;
  27. /**
  28. * 获取验证码
  29. *
  30. * @return string
  31. */
  32. public function get($config = array()) {
  33. // 初始化
  34. if (!$this->init($config)) {
  35. return false;
  36. }
  37. unset($config);
  38. // 生成验证码
  39. $captcha = $this->generate_captcha($this->length);
  40. if (false === $captcha) {
  41. return false;
  42. }
  43. // 背景
  44. $img = imagecreatetruecolor($this->width, $this->height);
  45. $color = imagecolorallocate($img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
  46. imagefilledrectangle($img, 0, $this->height, $this->width, 0, $color);
  47. // 线条
  48. for ($i = 0; $i < 6; ++$i) {
  49. $color = imagecolorallocate($img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  50. imageline(
  51. $img
  52. , mt_rand(0, $this->width), mt_rand(0, $this->height)
  53. , mt_rand(0, $this->width), mt_rand(0, $this->height)
  54. , $color
  55. );
  56. }
  57. // 雪花
  58. for ($i = 0; $i < 100; ++$i) {
  59. $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  60. imagestring($img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
  61. }
  62. // 验证码
  63. $j = $this->width / $this->length;
  64. for ($i = 0; $i < $this->length; ++$i) {
  65. $color = imagecolorallocate($img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  66. imagettftext(
  67. $img, $this->size, mt_rand(-30, 30)
  68. , $j * $i + mt_rand(1, 5), $this->height / 1.4
  69. , $color, $this->font
  70. , $captcha[$i]
  71. );
  72. }
  73. // 输出
  74. header('Content-type: image/png');
  75. imagepng($img);
  76. // 销毁
  77. imagedestroy($img);
  78. unset($img, $color, $i, $j);
  79. // 返回
  80. return strtolower($captcha);
  81. }
  82. /**
  83. * 初始化
  84. *
  85. * @param array $config
  86. *
  87. * @return bool
  88. */
  89. private function init($config) {
  90. if (is_array($config)) {
  91. if (isset($config['font']) && is_file($config['font']) && is_readable($config['font'])) {
  92. $this->font = $config['font'];
  93. } else {
  94. return false;
  95. }
  96. if (isset($config['size']) && ($config['size'] = (int) $config['size']) && 0 < $config['size']) {
  97. $this->size = $config['size'];
  98. }
  99. if (isset($config['width']) && ($config['width'] = (int) $config['width']) && 0 < $config['width']) {
  100. $this->width = $config['width'];
  101. }
  102. if (isset($config['height']) && ($config['height'] = (int) $config['height']) && 0 < $config['height']) {
  103. $this->height = $config['height'];
  104. }
  105. if (isset($config['length']) && ($config['length'] = (int) $config['length']) && 0 < $config['length']) {
  106. $this->length = $config['length'];
  107. }
  108. } else {
  109. return false;
  110. }
  111. return true;
  112. }
  113. /**
  114. * 生成验证码
  115. *
  116. * @return string
  117. */
  118. private function generate_captcha($length = 6) {
  119. $length = intval($length);
  120. if (1 > $length || 50 < $length) {
  121. return false;
  122. }
  123. $chars = array(
  124. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'k', 'm',
  125. 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  126. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'M',
  127. 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  128. '2', '3', '4', '5', '6', '7', '8', '9'
  129. );
  130. $keys = array_rand($chars, $length);
  131. $captcha = '';
  132. foreach ($keys as $key) {
  133. $captcha .= $chars[$key];
  134. }
  135. unset($length, $chars, $keys, $key);
  136. return $captcha;
  137. }
  138. }