/** * @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); }
/** * 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()); } }
<?php include '../CaptchaBuilderInterface.php'; include '../PhraseBuilderInterface.php'; include '../CaptchaBuilder.php'; include '../PhraseBuilder.php'; use Gregwar\Captcha\CaptchaBuilder; /** * Generates 1000 captchas and try to read their code with the * ocrad OCR */ $tests = 10000; $passed = 0; shell_exec('rm passed*.jpg'); for ($i = 0; $i < $tests; $i++) { echo "Captcha {$i}/{$tests}... "; $captcha = new CaptchaBuilder(); $captcha->setDistortion(false)->build(); if ($captcha->isOCRReadable()) { $passed++; $captcha->save("passed{$passed}.jpg"); echo "passed at ocr... "; } else { echo "failed... "; } echo "pass rate: " . round(100 * $passed / ($i + 1), 2) . "%\n"; } echo "\n"; echo "Over, {$passed}/{$tests} readed with OCR\n";