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; }
/** * 生成验证码. * * @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; }
/** * @param array $options * * @return string */ public function getPhrase(array &$options) { // Get the phrase that we'll use for this image if ($options['keep_value'] && isset($options['phrase'])) { $phrase = $options['phrase']; } else { $phrase = $this->phraseBuilder->build($options['length'], $options['charset']); $options['phrase'] = $phrase; } return $phrase; }