diff --git a/Generator/CaptchaGenerator.php b/Generator/CaptchaGenerator.php index c923497..82247e5 100644 --- a/Generator/CaptchaGenerator.php +++ b/Generator/CaptchaGenerator.php @@ -14,14 +14,17 @@ class CaptchaGenerator { $this->value = $value; } - public function getCode() + public function getCode($width = 120, $height = 40) { - return 'data:image/jpeg;base64,'.base64_encode($this->generate()); + return 'data:image/jpeg;base64,'.base64_encode($this->generate($width, $height)); } - - public function generate() + + /** + * Generate the image + */ + public function generate($width, $height) { - $i = imagecreatetruecolor(120,40); + $i = imagecreatetruecolor($width,$height); $col = imagecolorallocate($i, mt_rand(0,110), mt_rand(0,110), mt_rand(0,110)); @@ -83,7 +86,7 @@ class CaptchaGenerator { return ob_get_clean(); } - function getCol($image, $x, $y) + protected function getCol($image, $x, $y) { $L = imagesx($image); $H = imagesy($image); @@ -92,7 +95,7 @@ class CaptchaGenerator { else return imagecolorat($image, $x, $y); } - function getRGB($col) { + protected function getRGB($col) { return array( (int)($col >> 16) & 0xff, (int)($col >> 8) & 0xff, diff --git a/Resources/config/services.yml b/Resources/config/services.yml index b3d8334..a423810 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -3,6 +3,6 @@ services: # captcha type captcha.type: class: Gregwar\CaptchaBundle\Type\CaptchaType - arguments: [@session] + arguments: [@session, @service_container] tags: - { name: form.type, alias: captcha } diff --git a/Resources/views/captcha.html.twig b/Resources/views/captcha.html.twig index 7a44969..a3df35c 100644 --- a/Resources/views/captcha.html.twig +++ b/Resources/views/captcha.html.twig @@ -1,4 +1,7 @@ {% block captcha_widget %} - +{% spaceless %} + {{ form_widget(form) }} +{% endspaceless %} {% endblock %} + diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 94ff7cd..48a4d79 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -2,13 +2,14 @@ namespace Gregwar\CaptchaBundle\Type; +use Symfony\Component\HttpFoundation\Session; +use Symfony\Component\DependencyInjection\ContainerInterface; + use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; -use Symfony\Bridge\Doctrine\RegistryInterface; use Symfony\Component\Form\Exception\FormException; -use Symfony\Component\HttpFoundation\Session; use Gregwar\CaptchaBundle\Validator\CaptchaValidator; use Gregwar\CaptchaBundle\Generator\CaptchaGenerator; @@ -20,13 +21,33 @@ use Gregwar\CaptchaBundle\Generator\CaptchaGenerator; */ class CaptchaType extends AbstractType { + /** + * The image height + * @var integer + */ + protected $height; + + /** + * The image width + * @var integer + */ + protected $width; + + /** + * The session + * @var Symfony\Component\HttpFoundation\Session + */ + protected $session; + private $key = 'captcha'; - protected $session; - public function __construct(Session $session) + public function __construct(Session $session, ContainerInterface $container) { $this->session = $session; + $this->height = $container->getParameter('gregwar_captcha.image.height'); + $this->width = $container->getParameter('gregwar_captcha.image.width'); + } public function buildForm(FormBuilder $builder, array $options) @@ -40,7 +61,9 @@ class CaptchaType extends AbstractType { $generator = new CaptchaGenerator($this->generateCaptchaValue()); - $view->set('captcha_code', $generator->getCode()); + $view->set('captcha_code', $generator->getCode($this->width, $this->height)); + $view->set('captcha_width', $this->width); + $view->set('captcha_height', $this->height); } public function getParent(array $options)