心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Code.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2016年12月16日
  7. * 验证码生成类
  8. */
  9. namespace core\extend\code;
  10. class Code
  11. {
  12. // 随机因子
  13. public $charset = 'ABCDEFGHKMNPRTUVWXY23456789';
  14. // 指定字体大小
  15. public $fontsize = 18;
  16. // 验证码长度
  17. public $codelen = 4;
  18. // 宽度
  19. public $width = 130;
  20. // 高度
  21. public $height = 50;
  22. // 验证码
  23. private $code;
  24. // 图形资源句柄
  25. private $img;
  26. // 指定的字体
  27. private $font;
  28. // 指定字体颜色
  29. private $fontcolor;
  30. // 构造方法初始化
  31. public function __construct()
  32. {
  33. $this->font = dirname(__FILE__) . '/elephant.ttf';
  34. }
  35. // 生成随机码
  36. private function createCode()
  37. {
  38. $this->charset = str_shuffle($this->charset);
  39. $_len = strlen($this->charset) - 1;
  40. for ($i = 0; $i < $this->codelen; $i ++) {
  41. $this->code .= $this->charset[mt_rand(0, $_len)];
  42. }
  43. }
  44. // 生成背景
  45. private function createBg()
  46. {
  47. $this->img = imagecreatetruecolor($this->width, $this->height);
  48. $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  49. imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
  50. }
  51. // 生成文字
  52. private function createFont()
  53. {
  54. $_x = ($this->width - 10) / $this->codelen;
  55. for ($i = 0; $i < $this->codelen; $i ++) {
  56. $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  57. imagettftext($this->img, $this->fontsize, mt_rand(- 20, 20), $_x * $i + $_x / 3, $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
  58. }
  59. }
  60. // 生成线条、雪花
  61. private function createLine()
  62. {
  63. for ($i = 0; $i < 6; $i ++) {
  64. $color = imagecolorallocate($this->img, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));
  65. imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
  66. }
  67. for ($i = 0; $i < 100; $i ++) {
  68. $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  69. imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
  70. }
  71. }
  72. // 输出
  73. private function outPut()
  74. {
  75. header('Content-type:image/png');
  76. imagepng($this->img);
  77. imagedestroy($this->img);
  78. }
  79. // 对外生成
  80. public function doimg()
  81. {
  82. @ob_clean(); // 清理图片输出前内容,避免生成错误!
  83. $this->createBg();
  84. $this->createCode();
  85. $this->createLine();
  86. $this->createFont();
  87. $this->outPut();
  88. }
  89. // 获取验证码
  90. public function getCode()
  91. {
  92. return strtolower($this->code);
  93. }
  94. }