2011-08-25 23:10:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Generator;
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2012-11-14 04:33:36 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2012-12-03 20:49:17 +01:00
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
2011-11-09 14:43:25 +01:00
|
|
|
|
2013-01-21 14:01:31 +01:00
|
|
|
use Gregwar\Captcha\CaptchaBuilderInterface;
|
|
|
|
use Gregwar\Captcha\PhraseBuilderInterface;
|
|
|
|
|
2011-08-25 23:10:24 +02:00
|
|
|
/**
|
2012-12-04 01:02:43 +01:00
|
|
|
* Uses configuration parameters to call the services that generate captcha images
|
|
|
|
*
|
|
|
|
* @author Gregwar <g.passault@gmail.com>
|
|
|
|
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
|
2011-08-25 23:10:24 +02:00
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
class CaptchaGenerator
|
|
|
|
{
|
2012-12-03 20:49:17 +01:00
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\Routing\RouterInterface
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
2011-11-09 17:58:53 +01:00
|
|
|
/**
|
2012-12-04 11:51:47 +01:00
|
|
|
* @var CaptchaBuilder
|
2011-12-02 19:26:31 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
protected $builder;
|
2011-12-02 19:26:31 +01:00
|
|
|
|
2012-12-04 11:51:47 +01:00
|
|
|
/**
|
|
|
|
* @var PhraseBuilder
|
|
|
|
*/
|
|
|
|
protected $phraseBuilder;
|
|
|
|
|
2011-12-02 19:26:31 +01:00
|
|
|
/**
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var ImageFileHandler
|
2011-12-02 19:26:31 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
protected $imageFileHandler;
|
2011-12-02 19:26:31 +01:00
|
|
|
|
2011-12-02 19:07:28 +01:00
|
|
|
/**
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router
|
2013-01-21 14:01:31 +01:00
|
|
|
* @param CaptchaBuilderInterface $builder
|
|
|
|
* @param ImageFileHandlerInterface $imageFileHandler
|
2011-12-02 19:07:28 +01:00
|
|
|
*/
|
2013-01-21 15:18:36 +01:00
|
|
|
public function __construct(RouterInterface $router, CaptchaBuilderInterface $builder, PhraseBuilderInterface $phraseBuilder, ImageFileHandler $imageFileHandler)
|
2011-08-25 23:10:24 +02:00
|
|
|
{
|
2012-12-03 20:49:17 +01:00
|
|
|
$this->router = $router;
|
2012-12-04 00:48:32 +01:00
|
|
|
$this->builder = $builder;
|
2012-12-04 11:51:47 +01:00
|
|
|
$this->phraseBuilder = $phraseBuilder;
|
2013-01-21 15:18:36 +01:00
|
|
|
$this->imageFileHandler = $imageFileHandler;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
2011-11-09 14:43:25 +01:00
|
|
|
|
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* Get the captcha URL, stream, or filename that will go in the image's src attribute
|
|
|
|
*
|
|
|
|
* @param $key
|
|
|
|
* @param array $options
|
2011-11-09 14:43:25 +01:00
|
|
|
*
|
2012-11-14 04:33:36 +01:00
|
|
|
* @return array
|
2011-11-09 14:43:25 +01:00
|
|
|
*/
|
2013-01-21 15:18:36 +01:00
|
|
|
public function getCaptchaCode(array &$options)
|
2011-11-09 14:43:25 +01:00
|
|
|
{
|
2013-01-21 15:18:36 +01:00
|
|
|
$this->builder->setPhrase($this->getPhrase($options));
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
// Randomly execute garbage collection and returns the image filename
|
|
|
|
if ($options['as_file']) {
|
2012-12-04 00:48:32 +01:00
|
|
|
$this->imageFileHandler->collectGarbage();
|
2011-11-09 14:43:25 +01:00
|
|
|
|
2013-01-21 15:18:36 +01:00
|
|
|
return $this->generate($options);
|
2011-12-02 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
// Returns the image generation URL
|
2012-11-14 04:33:36 +01:00
|
|
|
if ($options['as_url']) {
|
2013-01-21 15:18:36 +01:00
|
|
|
return $this->router->generate('gregwar_captcha.generate_captcha', array('key' => $options['session_key']));
|
2011-12-02 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 15:18:36 +01:00
|
|
|
return 'data:image/jpeg;base64,' . base64_encode($this->generate($options));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the phrase to the builder
|
|
|
|
*/
|
|
|
|
public function setPhrase($phrase)
|
|
|
|
{
|
|
|
|
$this->builder->setPhrase($phrase);
|
2011-12-02 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
/**
|
2012-12-04 00:48:32 +01:00
|
|
|
* @param string $key
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return string
|
2011-11-09 14:43:25 +01:00
|
|
|
*/
|
2013-01-21 15:18:36 +01:00
|
|
|
public function generate(array &$options)
|
2011-11-09 14:43:25 +01:00
|
|
|
{
|
2013-01-21 14:04:53 +01:00
|
|
|
$this->builder->setDistortion($options['distortion']);
|
|
|
|
|
2013-03-13 11:02:41 +01:00
|
|
|
$this->builder->setMaxFrontLines($options['max_front_lines']);
|
|
|
|
$this->builder->setMaxBehindLines($options['max_behind_lines']);
|
|
|
|
|
2013-08-07 11:18:16 +02:00
|
|
|
if (isset($options['text_color']) && $options['text_color']) {
|
2013-04-29 04:13:58 +02:00
|
|
|
if (count($options['text_color']) !== 3) {
|
2013-04-29 04:17:04 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2013-08-07 11:18:16 +02:00
|
|
|
if (isset($options['background_color']) && $options['background_color']) {
|
2013-04-29 04:13:58 +02:00
|
|
|
if (count($options['background_color']) !== 3) {
|
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:32:40 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 00:41:07 +02:00
|
|
|
$color = $options['background_color'];
|
2013-04-22 00:32:40 +02:00
|
|
|
$this->builder->setBackgroundColor($color[0], $color[1], $color[2]);
|
|
|
|
}
|
|
|
|
|
2013-04-24 18:11:08 +02:00
|
|
|
$this->builder->setInterpolation($options['interpolation']);
|
|
|
|
|
2013-01-21 15:18:36 +01:00
|
|
|
$fingerprint = isset($options['fingerprint']) ? $options['fingerprint'] : null;
|
|
|
|
|
2013-09-26 20:38:39 +02:00
|
|
|
// set background images
|
|
|
|
$this->builder->setBackgroundImages($options['background_images']);
|
|
|
|
|
2013-09-26 21:10:02 +02:00
|
|
|
// set ignore all effects
|
|
|
|
$this->builder->setIgnoreAllEffects($options['ignore_all_effects']);
|
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
$content = $this->builder->build(
|
|
|
|
$options['width'],
|
|
|
|
$options['height'],
|
|
|
|
$options['font'],
|
|
|
|
$fingerprint
|
2013-01-21 14:01:31 +01:00
|
|
|
)->getGd();
|
2012-11-14 04:33:36 +01:00
|
|
|
|
|
|
|
if ($options['keep_value']) {
|
2013-01-21 15:18:36 +01:00
|
|
|
$options['fingerprint'] = $this->builder->getFingerprint();
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
2011-08-25 23:10:24 +02:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
if (!$options['as_file']) {
|
2011-11-09 14:43:25 +01:00
|
|
|
ob_start();
|
2012-12-04 00:48:32 +01:00
|
|
|
imagejpeg($content, null, $options['quality']);
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
return ob_get_clean();
|
|
|
|
}
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
return $this->imageFileHandler->saveAsFile($content);
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param string $key
|
2012-12-04 00:48:32 +01:00
|
|
|
* @param array $options
|
2012-11-14 04:33:36 +01:00
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @return string
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2013-01-21 15:18:36 +01:00
|
|
|
public function getPhrase(array &$options)
|
2011-08-25 23:10:24 +02:00
|
|
|
{
|
2012-12-04 00:48:32 +01:00
|
|
|
// Get the phrase that we'll use for this image
|
2013-01-21 15:18:36 +01:00
|
|
|
if ($options['keep_value'] && isset($options['phrase'])) {
|
|
|
|
$phrase = $options['phrase'];
|
|
|
|
} else {
|
|
|
|
$phrase = $this->phraseBuilder->build($options['length'], $options['charset']);
|
|
|
|
$options['phrase'] = $phrase;
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
2013-01-21 15:18:36 +01:00
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
return $phrase;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|