CaptchaBundle/Generator/CaptchaGenerator.php

144 lines
4.4 KiB
PHP
Raw Normal View History

2011-08-25 23:10:24 +02:00
<?php
declare(strict_types=1);
2011-08-25 23:10:24 +02:00
namespace Gregwar\CaptchaBundle\Generator;
2015-05-13 03:12:49 +02:00
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Symfony\Component\Routing\RouterInterface;
2013-01-21 14:01:31 +01:00
use Gregwar\Captcha\CaptchaBuilderInterface;
use Gregwar\Captcha\PhraseBuilderInterface;
2011-08-25 23:10:24 +02:00
/**
2020-01-03 00:12:44 +01:00
* Uses configuration parameters to call the services that generate captcha images.
2012-12-04 01:02:43 +01:00
*
* @author Gregwar <g.passault@gmail.com>
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
2011-08-25 23:10:24 +02:00
*/
class CaptchaGenerator
{
/** @var RouterInterface */
protected $router;
/** @var CaptchaBuilder */
protected $builder;
/** @var PhraseBuilder */
protected $phraseBuilder;
/** @var ImageFileHandler */
protected $imageFileHandler;
2011-12-02 19:07:28 +01:00
/**
2020-01-03 00:12:44 +01:00
* @param RouterInterface $router
2013-01-21 14:01:31 +01:00
* @param CaptchaBuilderInterface $builder
2020-01-03 00:12:44 +01:00
* @param PhraseBuilderInterface $phraseBuilder
* @param ImageFileHandler $imageFileHandler
2011-12-02 19:07:28 +01:00
*/
2015-05-13 03:12:49 +02:00
public function __construct(
RouterInterface $router,
CaptchaBuilderInterface $builder,
PhraseBuilderInterface $phraseBuilder,
ImageFileHandler $imageFileHandler
) {
2020-01-03 00:12:44 +01:00
$this->router = $router;
$this->builder = $builder;
$this->phraseBuilder = $phraseBuilder;
$this->imageFileHandler = $imageFileHandler;
2011-08-25 23:10:24 +02:00
}
public function getCaptchaCode(array &$options): string
{
$this->builder->setPhrase($this->getPhrase($options));
// Randomly execute garbage collection and returns the image filename
if ($options['as_file']) {
$this->imageFileHandler->collectGarbage();
return $this->generate($options);
}
// Returns the image generation URL
if ($options['as_url']) {
2020-01-03 00:12:44 +01:00
return $this->router->generate(
'gregwar_captcha.generate_captcha',
array('key' => $options['session_key'], 'n' => md5(microtime(true).mt_rand()))
);
}
2020-01-03 00:12:44 +01:00
return 'data:image/jpeg;base64,'.base64_encode($this->generate($options));
}
public function setPhrase(string $phrase): void
{
$this->builder->setPhrase($phrase);
}
public function generate(array &$options): string
{
2013-01-21 14:04:53 +01:00
$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']) {
2020-01-03 00:12:44 +01:00
if (3 !== count($options['text_color'])) {
throw new \RuntimeException('text_color should be an array of r, g and b');
2013-04-29 02:50:17 +02:00
}
$color = $options['text_color'];
$this->builder->setTextColor($color[0], $color[1], $color[2]);
}
if (isset($options['background_color']) && $options['background_color']) {
2020-01-03 00:12:44 +01:00
if (3 !== count($options['background_color'])) {
2013-04-22 00:41:07 +02:00
throw new \RuntimeException('background_color should be an array of r, g and b');
}
2013-04-22 00:41:07 +02:00
$color = $options['background_color'];
$this->builder->setBackgroundColor($color[0], $color[1], $color[2]);
}
2013-04-24 18:11:08 +02:00
$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
2013-01-21 14:01:31 +01:00
)->getGd();
if ($options['keep_value']) {
$options['fingerprint'] = $this->builder->getFingerprint();
}
2011-08-25 23:10:24 +02:00
if (!$options['as_file']) {
ob_start();
imagejpeg($content, null, $options['quality']);
return ob_get_clean();
}
return $this->imageFileHandler->saveAsFile($content);
2011-08-25 23:10:24 +02:00
}
public function getPhrase(array &$options): string
2011-08-25 23:10:24 +02:00
{
// 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;
}
2020-01-03 00:12:44 +01:00
return $phrase;
2011-08-25 23:10:24 +02:00
}
}