Added captcha image width and height as a configuration setting.

This commit is contained in:
Gregory McLean 2011-09-08 11:07:04 -04:00
parent 1afeeb8733
commit e2a705dda7
4 changed files with 43 additions and 14 deletions

View File

@ -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,

View File

@ -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 }

View File

@ -1,4 +1,7 @@
{% block captcha_widget %}
<img src="{{ captcha_code }}" title="captcha" width="120" height="40" />
{% spaceless %}
<img src="{{ captcha_code }}" title="captcha" width="{{ captcha_width }}" height="{{ captcha_height }}" />
{{ form_widget(form) }}
{% endspaceless %}
{% endblock %}

View File

@ -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)