Added captcha image width and height as a configuration setting.
This commit is contained in:
parent
1afeeb8733
commit
e2a705dda7
|
@ -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,
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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 %}
|
||||
|
||||
|
|
|
@ -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
|
||||
{
|
||||
private $key = 'captcha';
|
||||
/**
|
||||
* The image height
|
||||
* @var integer
|
||||
*/
|
||||
protected $height;
|
||||
|
||||
/**
|
||||
* The image width
|
||||
* @var integer
|
||||
*/
|
||||
protected $width;
|
||||
|
||||
/**
|
||||
* The session
|
||||
* @var Symfony\Component\HttpFoundation\Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
public function __construct(Session $session)
|
||||
private $key = 'captcha';
|
||||
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue