Captcha.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Constants;
  3. class Captcha
  4. {
  5. private $width;
  6. private $height;
  7. private $codeNum;
  8. private $code;
  9. private $im;
  10. //初始化
  11. function __construct($width = 80, $height = 20, $codeNum = 4)
  12. {
  13. $this->width = $width;
  14. $this->height = $height;
  15. $this->codeNum = $codeNum;
  16. }
  17. //显示验证码
  18. function showImg()
  19. {
  20. //创建图片
  21. $this->createImg();
  22. //设置干扰元素
  23. $this->setDisturb();
  24. //设置验证码
  25. $this->setCaptcha();
  26. //输出图片
  27. // $this->outputImg();
  28. }
  29. //获取显示的验证码,用来验证验证码是否数据正确
  30. function getCaptcha()
  31. {
  32. return $this->code;
  33. }
  34. //创建图片
  35. private function createImg()
  36. {
  37. $this->im = imagecreatetruecolor($this->width, $this->height);
  38. $bgColor = imagecolorallocate($this->im, 255, 255, 255);//创建的前景为白色
  39. imagefill($this->im, 0, 0, $bgColor);
  40. }
  41. //设置干扰元素
  42. private function setDisturb()
  43. {
  44. $area = ($this->width * $this->height) / 20;
  45. $disturbNum = ($area > 250) ? 250 : $area;
  46. //加入点干扰
  47. for ($i = 0; $i < $disturbNum; $i++) {
  48. $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
  49. imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
  50. }
  51. //加入弧线
  52. for ($i = 0; $i <= 1; $i++) {//最多两条线
  53. $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
  54. imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
  55. }
  56. }
  57. //设置验证码随机数
  58. private function createCode()
  59. {
  60. $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
  61. $str = str_split($str);
  62. for ($i = 0; $i < $this->codeNum; $i++) {
  63. $this->code .= $str[rand(0, sizeof($str) - 1)];
  64. // $this->code .= $str{rand(0, strlen($str) - 1)};
  65. }
  66. }
  67. //设置验证码
  68. private function setCaptcha()
  69. {
  70. //设置验证码随机数
  71. $this->createCode();
  72. //文字颜色
  73. $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
  74. //字体文件
  75. $fontPath = base_path() . "/public/layuiadmin/layui/font/Monaco.ttf";
  76. //图象资源,尺寸,角度,x轴,y轴,颜色,字体路径,文本插入图像
  77. imagefttext($this->im, 30, 0, 10, 35, $color, $fontPath, $this->code);
  78. }
  79. //输出图片
  80. private function outputImg()
  81. {
  82. if (imagetypes() & IMG_JPG) {
  83. // 直接向浏览器输出图片
  84. header('Content-type:image/jpeg');
  85. imagejpeg($this->im);
  86. } elseif (imagetypes() & IMG_GIF) {
  87. header('Content-type: image/gif');
  88. imagegif($this->im);
  89. } elseif (imagetypes() & IMG_PNG) {
  90. header('Content-type: image/png');
  91. imagepng($this->im);
  92. } else {
  93. die("Don't support image type!");
  94. }
  95. }
  96. public function getBase64Image()
  97. {
  98. ob_start();
  99. imagepng($this->im);
  100. $imageData = ob_get_contents();
  101. ob_end_clean();
  102. $base64Image = base64_encode($imageData);
  103. $base64Image = "data:image/png;base64," . $base64Image;
  104. imagedestroy($this->im);
  105. return $base64Image;
  106. }
  107. }