vendor/gregwar/captcha-bundle/Generator/CaptchaGenerator.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Gregwar\CaptchaBundle\Generator;
  4. use Gregwar\Captcha\CaptchaBuilder;
  5. use Gregwar\Captcha\PhraseBuilder;
  6. use Symfony\Component\Routing\RouterInterface;
  7. /**
  8.  * Uses configuration parameters to call the services that generate captcha images.
  9.  *
  10.  * @author Gregwar <g.passault@gmail.com>
  11.  * @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
  12.  */
  13. class CaptchaGenerator
  14. {
  15.     /** @var RouterInterface */
  16.     protected $router;
  17.     /** @var CaptchaBuilder */
  18.     protected $builder;
  19.     /** @var PhraseBuilder */
  20.     protected $phraseBuilder;
  21.     /** @var ImageFileHandler */
  22.     protected $imageFileHandler;
  23.     /**
  24.      * @param RouterInterface         $router
  25.      * @param CaptchaBuilder          $builder
  26.      * @param PhraseBuilder           $phraseBuilder
  27.      * @param ImageFileHandler        $imageFileHandler
  28.      */
  29.     public function __construct(
  30.         RouterInterface $router,
  31.         CaptchaBuilder $builder,
  32.         PhraseBuilder $phraseBuilder,
  33.         ImageFileHandler $imageFileHandler
  34.     ) {
  35.         $this->router $router;
  36.         $this->builder $builder;
  37.         $this->phraseBuilder $phraseBuilder;
  38.         $this->imageFileHandler $imageFileHandler;
  39.     }
  40.     /**
  41.      * @param array<mixed> $options
  42.      *
  43.      * @return string
  44.      */
  45.     public function getCaptchaCode(array &$options): string
  46.     {
  47.         $this->builder->setPhrase($this->getPhrase($options));
  48.         // Randomly execute garbage collection and returns the image filename
  49.         if ($options['as_file']) {
  50.             $this->imageFileHandler->collectGarbage();
  51.             return $this->generate($options);
  52.         }
  53.         // Returns the image generation URL
  54.         if ($options['as_url']) {
  55.             return $this->router->generate(
  56.                 'gregwar_captcha.generate_captcha',
  57.                 array('key' => $options['session_key'], 'n' => md5(microtime(true).mt_rand()))
  58.             );
  59.         }
  60.         return 'data:image/jpeg;base64,'.base64_encode($this->generate($options));
  61.     }
  62.     public function setPhrase(string $phrase): void
  63.     {
  64.         $this->builder->setPhrase($phrase);
  65.     }
  66.     /**
  67.      * @param array<mixed> $options
  68.      *
  69.      * @return string
  70.      */
  71.     public function generate(array &$options): string
  72.     {
  73.         $this->builder->setDistortion($options['distortion']);
  74.         $this->builder->setMaxFrontLines($options['max_front_lines']);
  75.         $this->builder->setMaxBehindLines($options['max_behind_lines']);
  76.         if (isset($options['text_color']) && $options['text_color']) {
  77.             if (!== count($options['text_color'])) {
  78.                 throw new \RuntimeException('text_color should be an array of r, g and b');
  79.             }
  80.             $color $options['text_color'];
  81.             $this->builder->setTextColor($color[0], $color[1], $color[2]);
  82.         }
  83.         if (isset($options['background_color']) && $options['background_color']) {
  84.             if (!== count($options['background_color'])) {
  85.                 throw new \RuntimeException('background_color should be an array of r, g and b');
  86.             }
  87.             $color $options['background_color'];
  88.             $this->builder->setBackgroundColor($color[0], $color[1], $color[2]);
  89.         }
  90.         $this->builder->setInterpolation($options['interpolation']);
  91.         $fingerprint = isset($options['fingerprint']) ? $options['fingerprint'] : null;
  92.         $this->builder->setBackgroundImages($options['background_images']);
  93.         $this->builder->setIgnoreAllEffects($options['ignore_all_effects']);
  94.         $content $this->builder->build(
  95.             $options['width'],
  96.             $options['height'],
  97.             $options['font'],
  98.             $fingerprint
  99.         )->getGd();
  100.         if ($options['keep_value']) {
  101.             $options['fingerprint'] = $this->builder->getFingerprint();
  102.         }
  103.         if (!$options['as_file']) {
  104.             ob_start();
  105.             imagejpeg($contentnull$options['quality']);
  106.             $bufferContents ob_get_clean();
  107.             return false === $bufferContents '' $bufferContents;
  108.         }
  109.         return $this->imageFileHandler->saveAsFile($content);
  110.     }
  111.     /**
  112.      * @param array<mixed> $options
  113.      *
  114.      * @return string
  115.      */
  116.     public function getPhrase(array &$options): string
  117.     {
  118.         // Get the phrase that we'll use for this image
  119.         if ($options['keep_value'] && isset($options['phrase'])) {
  120.             $phrase $options['phrase'];
  121.         } else {
  122.             $phrase $this->phraseBuilder->build($options['length'], $options['charset']);
  123.             $options['phrase'] = $phrase;
  124.         }
  125.         return $phrase;
  126.     }
  127. }