config = $config; $this->cache = $cache; $this->configure(); } /** * 生成短信验证码并把验证码的值保存的缓存中 * @access public * @param string $phone * @return object */ public function createSMS(string $phone): array { $code = (string)mt_rand(100000, 999999); $this->cache->set("captchaSMS.{$phone}", ['code' => $code, 'times' => $this->checkTimes], $this->expire); return ['key' => $phone, 'code' => $code]; } /** * 验证短信验证码是否正确 * @access public * @param string $code * @param string $phone * @return object */ public function checkSMS(string $code, string $phone): bool { $captcha = $this->cache->get("captchaSMS.{$phone}"); if (empty($captcha)) { $this->throwError('短信验证码不存在,请重新获取'); } if ($captcha['times'] <= 0) { $this->cache->delete("captchaSMS.{$phone}"); $this->throwError('短信验证码已超出错误次数,请重新获取'); } if ($captcha['code'] != $code) { $this->cache->update("captchaSMS.{$phone}", ['times' => $captcha['times'] - 1]); $this->throwError('短信验证码不正确,请重新填写'); } $this->cache->delete("captchaSMS.{$phone}"); return true; } /** * 验证图形验证码是否正确 * @access public * @param string $code 用户验证码 * @param string $key 用户验证码key * @return bool 用户验证码是否正确 * @throws Exception */ public function check(string $code, string $key): bool { $captcha = $this->cache->get("captchaApi.{$key}"); if (empty($captcha)) { $this->throwError('图形验证码不存在,请重新获取'); } if ($captcha['times'] <= 0) { $this->cache->delete("captchaApi.{$key}"); $this->throwError('图形验证码已超出错误次数,请重新获取'); } if (password_verify(mb_strtolower($code, 'UTF-8'), $key) === false) { $this->cache->update("captchaApi.{$key}", ['times' => $captcha['times'] - 1]); return false; } $this->cache->delete("captchaApi.{$key}"); return true; } /** * 生成图形验证码并把验证码的值保存的缓存中 * @access public * @param null|string $config * @param bool $api * @return object * @throws Exception */ public function create(string $config = null, bool $api = false): array { $generator = $this->generate(); // 图片宽(px) $this->imageW || $this->imageW = $this->length * $this->fontSize * 1.5 + $this->length * $this->fontSize / 2; // 图片高(px) $this->imageH || $this->imageH = $this->fontSize * 2.5; // 建立一幅 $this->imageW x $this->imageH 的图像 $this->im = imagecreate($this->imageW, $this->imageH); // 设置背景 imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]); // 验证码字体随机颜色 $this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)); // 验证码使用随机字体 $ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/'; if (empty($this->fontttf)) { $dir = dir($ttfPath); $ttfs = []; while (false !== ($file = $dir->read())) { if ('.' != $file[0] && substr($file, -4) == '.ttf') { $ttfs[] = $file; } } $dir->close(); $this->fontttf = $ttfs[array_rand($ttfs)]; } $fontttf = $ttfPath . $this->fontttf; if ($this->useImgBg) { $this->background(); } if ($this->useNoise) { // 绘杂点 $this->writeNoise(); } if ($this->useCurve) { // 绘干扰线 $this->writeCurve(); } // 绘验证码 $text = $this->useZh ? preg_split('/(? $char) { $x = $this->fontSize * ($index + 1) * ($this->math ? 1 : 1.5); $y = $this->fontSize + mt_rand(10, 20); $angle = $this->math ? 0 : mt_rand(-40, 40); imagettftext($this->im, $this->fontSize, $angle, $x, $y, $this->color, $fontttf, $char); } ob_start(); // 输出图像 imagepng($this->im); $content = ob_get_clean(); imagedestroy($this->im); return [ 'base64' => 'data:image/png;base64,' . chunk_split(base64_encode($content)), 'key' => $generator['key'], 'code' => $generator['code'], 'md5' => $generator['md5'], ]; } /** * 配置验证码 * @param string|null $config */ protected function configure(string $config = null): void { if (is_null($config)) { $config = $this->config->get('captcha', []); } else { $config = $this->config->get('captcha.' . $config, []); } foreach ($config as $key => $val) { if (property_exists($this, $key)) { $this->{$key} = $val; } } } /** * 创建图形验证码的值 * @return array * @throws Exception */ protected function generate(): array { $bag = ''; if ($this->math) { $this->useZh = false; $this->length = 5; $x = random_int(10, 30); $y = random_int(1, 9); $bag = "{$x} + {$y} = "; $key = $x + $y; $key .= ''; } else { if ($this->useZh) { $characters = preg_split('/(?zhSet); } else { $characters = str_split($this->codeSet); } for ($i = 0; $i < $this->length; $i++) { $bag .= $characters[rand(0, count($characters) - 1)]; } $key = mb_strtolower($bag, 'UTF-8'); } $hash = password_hash($key, PASSWORD_BCRYPT, ['cost' => 10]); $this->cache->set("captchaApi.{$hash}", ['code' => $key, 'times' => $this->checkTimes], $this->expire); return [ 'value' => $bag, 'key' => $hash, 'code' => $key, 'md5' => md5($key), ]; } /** * 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数) * * 高中的数学公式咋都忘了涅,写出来 * 正弦型函数解析式:y=Asin(ωx+φ)+b * 各常数值对函数图像的影响: * A:决定峰值(即纵向拉伸压缩的倍数) * b:表示波形在Y轴的位置关系或纵向移动距离(上加下减) * φ:决定波形与X轴位置关系或横向移动距离(左加右减) * ω:决定周期(最小正周期T=2π/∣ω∣) * */ protected function writeCurve(): void { $px = $py = 0; // 曲线前部分 $A = mt_rand(1, $this->imageH / 2); // 振幅 $b = mt_rand(-$this->imageH / 4, $this->imageH / 4); // Y轴方向偏移量 $f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量 $T = mt_rand($this->imageH, $this->imageW * 2); // 周期 $w = (2 * M_PI) / $T; $px1 = 0; // 曲线横坐标起始位置 $px2 = mt_rand($this->imageW / 2, $this->imageW * 0.8); // 曲线横坐标结束位置 for ($px = $px1; $px <= $px2; $px = $px + 1) { if (0 != $w) { $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b $i = (int)($this->fontSize / 5); while ($i > 0) { imagesetpixel($this->im, $px + $i, $py + $i, $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多 $i--; } } } // 曲线后部分 $A = mt_rand(1, $this->imageH / 2); // 振幅 $f = mt_rand(-$this->imageH / 4, $this->imageH / 4); // X轴方向偏移量 $T = mt_rand($this->imageH, $this->imageW * 2); // 周期 $w = (2 * M_PI) / $T; $b = $py - $A * sin($w * $px + $f) - $this->imageH / 2; $px1 = $px2; $px2 = $this->imageW; for ($px = $px1; $px <= $px2; $px = $px + 1) { if (0 != $w) { $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b $i = (int)($this->fontSize / 5); while ($i > 0) { imagesetpixel($this->im, $px + $i, $py + $i, $this->color); $i--; } } } } /** * 画杂点 * 往图片上写不同颜色的字母或数字 */ protected function writeNoise(): void { $codeSet = '2345678abcdefhijkmnpqrstuvwxyz'; for ($i = 0; $i < 10; $i++) { //杂点颜色 $noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225)); for ($j = 0; $j < 5; $j++) { // 绘杂点 imagestring($this->im, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor); } } } /** * 绘制背景图片 * 注:如果验证码输出图片比较大,将占用比较多的系统资源 */ protected function background(): void { $path = __DIR__ . '/../assets/bgs/'; $dir = dir($path); $bgs = []; while (false !== ($file = $dir->read())) { if ('.' != $file[0] && substr($file, -4) == '.jpg') { $bgs[] = $path . $file; } } $dir->close(); $gb = $bgs[array_rand($bgs)]; list($width, $height) = @getimagesize($gb); // Resample $bgImage = @imagecreatefromjpeg($gb); @imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height); @imagedestroy($bgImage); } /** * 输出报错信息 * @param string $message * @throws Exception */ private function throwError(string $message) { throw new Exception($message); } }