public function build($name, array $options = []) { $width = isset($options['width']) ? $options['width'] : 150; $height = isset($options['height']) ? $options['height'] : 40; $font = isset($options['font']) ? $options['font'] : null; $phraseBuilder = new PhraseBuilder(); if (isset($options['captcha'])) { $captcha = $options['captcha']; } elseif (isset($options['length'])) { $captcha = $phraseBuilder->build($options['length']); } else { $captcha = null; } $captchaBuilder = new CaptchaBuilder($captcha, $phraseBuilder); if (isset($options['text_color'])) { $captchaBuilder->setBackgroundColor($options['text_color'][0], $options['text_color'][1], $options['text_color'][2]); } if (isset($options['background_color'])) { $captchaBuilder->setBackgroundColor($options['background_color'][0], $options['background_color'][1], $options['background_color'][2]); } if (isset($options['background_images'])) { $captchaBuilder->setBackgroundColor($options['background_image']); } if (isset($options['ignore_effect'])) { $captchaBuilder->setIgnoreAllEffects($options['ignore_effect']); } $captchaBuilder->build($width, $height, $font); $captcha = $captchaBuilder->getPhrase(); session(['image_captcha.' . $name => $captcha]); $this->captchaBuilder = $captchaBuilder; return $this; }
/** * Load connection parameters from config file. * * @access private * @param string $connection Connection group name */ private function setBuilder($width = 150, $height = 40, $font = null) { if ($this->session->has($this->secret . '::phrase') && $this->isAlive()) { // Get phrase saved in session $sessionPhrase = $this->session->get($this->secret . '::phrase'); // Decrypt phrase $phrase = $this->encrypter->decrypt($sessionPhrase); // Captcha builder $this->builder = new CaptchaBuilder($phrase); $this->builder->setDistortion(false); $this->builder->setMaxBehindLines(0); $this->builder->setMaxFrontLines(0); $this->builder->setIgnoreAllEffects(false); $this->builder->setBackgroundColor(241, 241, 241); $this->builder->build($width, $height, $font); } else { // Captcha builder $this->builder = new CaptchaBuilder(); $this->builder->setDistortion(false); $this->builder->setMaxBehindLines(0); $this->builder->setMaxFrontLines(0); $this->builder->setIgnoreAllEffects(false); $this->builder->setBackgroundColor(241, 241, 241); $this->builder->build($width, $height, $font); // Set last generated phrase $this->session->put($this->secret . '::phrase', $this->encrypter->encrypt($this->builder->getPhrase())); // Set last modified time $this->session->put($this->secret . '::lastModified', time()); } }
/** * @param array $options * * @return string */ public function generate(array &$options) { $this->builder->setDistortion($options['distortion']); $this->builder->setMaxFrontLines($options['max_front_lines']); $this->builder->setMaxBehindLines($options['max_behind_lines']); if (isset($options['text_color']) && $options['text_color']) { if (count($options['text_color']) !== 3) { throw new \RuntimeException('text_color should be an array of r, g and b'); } $color = $options['text_color']; $this->builder->setTextColor($color[0], $color[1], $color[2]); } if (isset($options['background_color']) && $options['background_color']) { if (count($options['background_color']) !== 3) { throw new \RuntimeException('background_color should be an array of r, g and b'); } $color = $options['background_color']; $this->builder->setBackgroundColor($color[0], $color[1], $color[2]); } $this->builder->setInterpolation($options['interpolation']); $fingerprint = isset($options['fingerprint']) ? $options['fingerprint'] : null; $this->builder->setBackgroundImages($options['background_images']); $this->builder->setIgnoreAllEffects($options['ignore_all_effects']); $content = $this->builder->build($options['width'], $options['height'], $options['font'], $fingerprint)->getGd(); if ($options['keep_value']) { $options['fingerprint'] = $this->builder->getFingerprint(); } if (!$options['as_file']) { ob_start(); imagejpeg($content, null, $options['quality']); return ob_get_clean(); } return $this->imageFileHandler->saveAsFile($content); }
/** * 生成验证码. * * @Route("captcha", name="site_common_captcha") */ public function captchaAction() { $width = intval($this->getRequestItem('width', 150)); // 验证码图片的宽度 $height = intval($this->getRequestItem('height', 50)); // 验证码图片的高度 $length = 5; // 验证码字符的长度 $no_effect = true; // 是否忽略验证图片上的干扰线条 $pharse = new PhraseBuilder(); $captcha = new CaptchaBuilder($pharse->build($length)); $image = $captcha->setIgnoreAllEffects($no_effect)->build($width, $height)->get(); $this->setSessionItem('captcha', $captcha->getPhrase()); $res = new Response($image); $res->headers->set('content-type', 'image/jpeg'); return $res; }