VerificationCode.Class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Mall\Framework\Core;
  3. class VerificationCode{
  4. private $width;
  5. private $height;
  6. private $codeNum;
  7. private $code;
  8. private $im;
  9. protected static $_instance;
  10. function __construct($width=80, $height=20, $codeNum=4)
  11. {
  12. $this->width = $width;
  13. $this->height = $height;
  14. $this->codeNum = $codeNum;
  15. }
  16. public static function getInstance()
  17. {
  18. $key = md5('VerificationCode');
  19. if (!isset(self::$_instance[$key])) {
  20. self::$_instance[$key] = new self();
  21. }
  22. return self::$_instance[$key];
  23. }
  24. //输出图像
  25. public function showImg()
  26. {
  27. //创建图片
  28. $this->createImg();
  29. //设置干扰元素
  30. $this->setDisturb();
  31. //设置验证码
  32. $this->setCaptcha();
  33. //输出图片
  34. $this->outputImg();
  35. }
  36. //获取验证码内容
  37. public function getCaptcha()
  38. {
  39. return $this->code;
  40. }
  41. private function createImg()
  42. {
  43. $this->im = imagecreatetruecolor($this->width, $this->height);
  44. $bgColor = imagecolorallocate($this->im, 0, 0, 0);
  45. imagefill($this->im, 0, 0, $bgColor);
  46. }
  47. private function setDisturb()
  48. {
  49. $area = ($this->width * $this->height) / 20;
  50. $disturbNum = ($area > 250) ? 250 : $area;
  51. //加入点干扰
  52. for ($i = 0; $i < $disturbNum; $i++) {
  53. $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
  54. imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
  55. }
  56. //加入弧线
  57. for ($i = 0; $i <= 5; $i++) {
  58. $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
  59. imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
  60. }
  61. }
  62. private function createCode()
  63. {
  64. $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
  65. for ($i = 0; $i < $this->codeNum; $i++) {
  66. $this->code .= $str{rand(0, strlen($str) - 1)};
  67. }
  68. }
  69. private function setCaptcha()
  70. {
  71. $this->createCode();
  72. for ($i = 0; $i < $this->codeNum; $i++) {
  73. $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
  74. $size = rand(floor($this->height / 5), floor($this->height / 3));
  75. $x = floor($this->width / $this->codeNum) * $i + 5;
  76. $y = rand(0, $this->height - 20);
  77. imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
  78. }
  79. }
  80. private function outputImg()
  81. {
  82. if (imagetypes() & IMG_JPG) {
  83. header('Content-type:image/jpeg');
  84. imagejpeg($this->im);
  85. } elseif (imagetypes() & IMG_GIF) {
  86. header('Content-type: image/gif');
  87. imagegif($this->im);
  88. } elseif (imagetypes() & IMG_PNG) {
  89. header('Content-type: image/png');
  90. imagepng($this->im);
  91. } else {
  92. die("Don't support image type!");
  93. }
  94. }
  95. }