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; }
public function init() { parent::init(); $this->provider = new CaptchaBuilder($this->generatePhrase()); if (!empty($this->backgroundColor)) { list($red, $green, $blue) = $this->backgroundColor; $this->provider->setBackgroundColor($red, $green, $blue); } }
/** * @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; $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); }
protected function renderImage($code) { $builder = new CaptchaBuilder($code); $builder->setBackgroundColor(255, 255, 255); $builder->build(150, 40, $this->fontFile); return $builder->get(); }
/** * 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()); } }
/** * 生成登录页面的图形验证码 * * @author young * @name 生成登录页面的图形验证码 * @version 2013.11.07 young * @return \Zend\Stdlib\ResponseInterface */ public function captchaAction() { $builder = new CaptchaBuilder(); $builder->setBackgroundColor(255, 255, 255); $builder->setTextColor(255, 0, 255); $builder->setPhrase(rand(100000, 999999)); $_SESSION['phrase'] = $builder->getPhrase(); $builder->build(150, 40); header('Content-type: image/jpeg'); $this->response->setContent($builder->output(80)); return $this->response; }