PHP code 验证码生成类定义和简单使用示例

这篇文章主要介绍了PHP code 验证码生成类定义和简单使用,结合实例形式分析了PHP code 验证码生成类的基本功能定义、简单使用方法及操作注意事项,需要的朋友可以参考下。

本文实例讲述了PHP code 验证码生成类定义和简单使用,分享给大家供大家参考,具体如下:

code.php

  1. <?php
  2. namespace code;
  3. /**
  4. * Class Code
  5. */
  6. class Code
  7. {
  8. protected $number;//验证码内字符个数
  9. protected $codeType;//验证码样式
  10. protected $width;//图像宽
  11. protected $height;//图像高
  12. protected $code;//验证码
  13. protected $image;//图像资源
  14. /**
  15. * Code constructor.
  16. * @param int $number
  17. * @param int $codeType
  18. * @param int $width
  19. * @param int $height
  20. */
  21. public function __construct($number=5, $codeType=2, $width=100, $height=40)
  22. {
  23. $this->number = $number;
  24. $this->codeType = $codeType;
  25. $this->width = $width;
  26. $this->height = $height;
  27. $this->code = $this->createCode();
  28. }
  29. /**
  30. * 销毁资源
  31. */
  32. public function __destruct()
  33. {
  34. imagedestroy($this->image);
  35. }
  36. /**
  37. * 外部调用code时触发
  38. * @param $name
  39. * @return bool
  40. */
  41. public function __get($name)
  42. {
  43. if ('code' == $name) {
  44. return $this->$name;
  45. } else {
  46. return false;
  47. }
  48. }
  49. /**
  50. * 生成code
  51. */
  52. protected function createCode()
  53. {
  54. switch ($this->codeType) {
  55. case 0:
  56. $code = $this->getNum();
  57. break;
  58. case 1:
  59. $code = $this->getChar();
  60. break;
  61. case 2:
  62. $code = $this->getNumChar();
  63. break;
  64. default:
  65. die('样式不对');
  66. }
  67. return $code;
  68. }
  69. /**
  70. * 数字验证码
  71. * @return string
  72. */
  73. protected function getNum()
  74. {
  75. $str = join('', range(0,9));
  76. return substr(str_shuffle($str), 0, $this->number);
  77. }
  78. /**
  79. * 字符验证码
  80. * @return string
  81. */
  82. protected function getChar()
  83. {
  84. $str = join('', range('a', 'z'));
  85. $str = $str . strtoupper($str);
  86. return substr(str_shuffle($str), 0, $this->number);
  87. }
  88. /**
  89. * 字符和数字混合验证码
  90. * @return string
  91. */
  92. protected function getNumChar()
  93. {
  94. $num = join('', range(0, 9));
  95. $str = join('', range('a', 'z'));
  96. $str_big = strtoupper($str);
  97. $numChar = $num . $str . $str_big;
  98. return substr(str_shuffle($numChar), 0, $this->number);
  99. }
  100. /**
  101. * 生成图像
  102. */
  103. protected function createImage()
  104. {
  105. $this->image = imagecreatetruecolor($this->width, $this->height);
  106. }
  107. /**
  108. * 填充背景色
  109. */
  110. protected function fillColor()
  111. {
  112. imagefill($this->image, 0, 0, $this->lightColor());
  113. }
  114. /**
  115. * 浅颜色
  116. * @return int
  117. */
  118. protected function lightColor()
  119. {
  120. return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255));
  121. }
  122. /**
  123. * 深颜色
  124. * @return int
  125. */
  126. protected function darkColor()
  127. {
  128. return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
  129. }
  130. /**
  131. * 添加验证码字符
  132. */
  133. protected function drawChar()
  134. {
  135. $width = ceil($this->width/$this->number);
  136. for ($i = 0; $i < $this->number; $i++) {
  137. $x = mt_rand($i * ($width - 5), ($i + 1) * ($width - 5));
  138. $y = mt_rand(0, $this->height - 15);
  139. imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
  140. }
  141. }
  142. /**
  143. * 添加干扰点
  144. */
  145. protected function drawDisturb()
  146. {
  147. for ($i= 0; $i < 100; $i++) {
  148. imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor());
  149. }
  150. }
  151. /**
  152. * 添加干扰线
  153. */
  154. protected function drawArc()
  155. {
  156. for ($i = 0; $i < $this->number - 3; $i++) {
  157. imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkColor());
  158. }
  159. }
  160. /**
  161. * 输出显示
  162. */
  163. protected function show()
  164. {
  165. header('Content-Type:image/png');
  166. imagepng($this->image);
  167. }
  168. /**
  169. * 外部image
  170. */
  171. public function outImage()
  172. {
  173. $this->createImage();//创建画布
  174. $this->fillColor();//填充背景色
  175. $this->drawChar();//添加验证字符
  176. $this->drawDisturb();//添加干扰点
  177. $this->drawArc();//添加干扰线
  178. $this->show();//输出
  179. }
  180. }

展示验证码,保存验证码和过期时间:

  1. <?php
  2. include './code/Code.php';
  3. $code = new code\Code();
  4. $code->outImage();
  5. session_start();
  6. $_SESSION['code'] = [
  7. 'code' => $code->code,
  8. 'exp_time' => time() + (60 * 60 * 10),
  9. ];