Captcha.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\helper;
  10. use fecshop\services\Service;
  11. /**
  12. * Helper Captcha services. 验证码部分
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class Captcha extends Service
  17. {
  18. public $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ0123456789'; //随机因子
  19. public $codelen = 4; //验证码长度
  20. public $width = 130; //宽度
  21. public $height = 50; //高度
  22. public $fontsize= 20; //指定字体大小
  23. public $case_sensitive = false;
  24. private $fontcolor; //指定字体颜色
  25. private $code; //验证码
  26. private $img; //图形资源句柄
  27. private $font; //指定的字体
  28. private $_sessionKey = 'captcha_session_key';
  29. /**
  30. * 1. 生成图片,.
  31. */
  32. //构造方法初始化
  33. public function init()
  34. {
  35. parent::init();
  36. $this->font = dirname(__FILE__).'/captcha/Elephant.ttf'; //注意字体路径要写对,否则显示不了图片
  37. }
  38. //生成随机码
  39. private function createCode()
  40. {
  41. $_len = strlen($this->charset) - 1;
  42. for ($i = 0; $i < $this->codelen; $i++) {
  43. $this->code .= $this->charset[mt_rand(0, $_len)];
  44. }
  45. }
  46. //生成背景
  47. private function createBg()
  48. {
  49. $this->img = imagecreatetruecolor($this->width, $this->height);
  50. $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
  51. imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
  52. }
  53. //生成文字
  54. private function createFont()
  55. {
  56. $_x = $this->width / $this->codelen;
  57. for ($i = 0; $i < $this->codelen; $i++) {
  58. if (!$this->fontcolor) {
  59. $fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  60. } else {
  61. $fontcolor = $this->fontcolor;
  62. }
  63. imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $fontcolor, $this->font, $this->code[$i]);
  64. }
  65. }
  66. //生成线条、雪花
  67. private function createLine()
  68. {
  69. //线条
  70. for ($i = 0; $i < 6; $i++) {
  71. $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  72. imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
  73. }
  74. //雪花
  75. for ($i = 0; $i < 100; $i++) {
  76. $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  77. imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
  78. }
  79. }
  80. //输出
  81. private function outPut()
  82. {
  83. header('Content-type:image/jpg');
  84. imagepng($this->img);
  85. imagedestroy($this->img);
  86. }
  87. //对外生成
  88. public function doBase64img()
  89. {
  90. $this->createBg();
  91. $this->createCode();
  92. $this->createLine();
  93. $this->createFont();
  94. ob_start();
  95. imagepng($this->img);
  96. imagedestroy($this->img);
  97. $fileContent = ob_get_contents();
  98. ob_end_clean();
  99. $this->setSessionCode();
  100. return base64_encode($fileContent);
  101. }
  102. //对外生成
  103. public function doimg()
  104. {
  105. $this->createBg();
  106. $this->createCode();
  107. $this->createLine();
  108. $this->createFont();
  109. $this->setSessionCode();
  110. session_commit();
  111. $this->outPut();
  112. }
  113. public function setSessionCode()
  114. {
  115. $code = $this->getCode($this->code);
  116. \Yii::$service->session->set($this->_sessionKey, $code);
  117. }
  118. //获取验证码
  119. public function getCode($code)
  120. {
  121. if (!$this->case_sensitive) {
  122. return strtolower($code);
  123. } else {
  124. return $this->code;
  125. }
  126. }
  127. public function validateCaptcha($captchaData)
  128. {
  129. $captchaData = $this->getCode($captchaData);
  130. $sessionCaptchaData = \Yii::$service->session->get($this->_sessionKey);
  131. return ($captchaData === $sessionCaptchaData) ? true : false;
  132. }
  133. }