Added "length" option and changed the parameters names
This commit is contained in:
parent
4a5b2a4c48
commit
4e59281467
|
@ -17,15 +17,12 @@ class Configuration implements ConfigurationInterface
|
||||||
$rootNode = $treeBuilder->root('gregwar_captcha', 'array');
|
$rootNode = $treeBuilder->root('gregwar_captcha', 'array');
|
||||||
|
|
||||||
$rootNode
|
$rootNode
|
||||||
->children()
|
|
||||||
->arrayNode('image')
|
|
||||||
->addDefaultsIfNotSet()
|
->addDefaultsIfNotSet()
|
||||||
->children()
|
->children()
|
||||||
|
->scalarNode('length')->defaultValue(5)->end()
|
||||||
->scalarNode('width')->defaultValue(120)->end()
|
->scalarNode('width')->defaultValue(120)->end()
|
||||||
->scalarNode('height')->defaultValue(40)->end()
|
->scalarNode('height')->defaultValue(40)->end()
|
||||||
->end()
|
->end()
|
||||||
->end()
|
|
||||||
->end()
|
|
||||||
;
|
;
|
||||||
return $treeBuilder;
|
return $treeBuilder;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,10 @@ class GregwarCaptchaExtension extends Extension
|
||||||
$configuration = new Configuration();
|
$configuration = new Configuration();
|
||||||
$config = $this->processConfiguration($configuration, $configs);
|
$config = $this->processConfiguration($configuration, $configs);
|
||||||
|
|
||||||
$container->setParameter('gregwar_captcha.image.height', $config['image']['height']);
|
$container->setParameter('gregwar_captcha.length', $config['length']);
|
||||||
$container->setParameter('gregwar_captcha.image.width', $config['image']['width']);
|
$container->setParameter('gregwar_captcha.height', $config['height']);
|
||||||
|
$container->setParameter('gregwar_captcha.width', $config['width']);
|
||||||
|
|
||||||
$resources = $container->getParameter('twig.form.resources');
|
$resources = $container->getParameter('twig.form.resources');
|
||||||
$container->setParameter('twig.form.resources',array_merge(array('GregwarCaptchaBundle::captcha.html.twig'), $resources));
|
$container->setParameter('twig.form.resources',array_merge(array('GregwarCaptchaBundle::captcha.html.twig'), $resources));
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ class CaptchaGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write CAPTCHA text
|
// Write CAPTCHA text
|
||||||
$size = $width/5.0;
|
$size = $width/strlen($this->value);
|
||||||
$font = __DIR__.'/Font/captcha.ttf';
|
$font = __DIR__.'/Font/captcha.ttf';
|
||||||
$box = imagettfbbox($size, 0, $font, $this->value);
|
$box = imagettfbbox($size, 0, $font, $this->value);
|
||||||
$txt_width = $box[2] - $box[0];
|
$txt_width = $box[2] - $box[0];
|
||||||
|
|
|
@ -3,6 +3,6 @@ services:
|
||||||
# captcha type
|
# captcha type
|
||||||
captcha.type:
|
captcha.type:
|
||||||
class: Gregwar\CaptchaBundle\Type\CaptchaType
|
class: Gregwar\CaptchaBundle\Type\CaptchaType
|
||||||
arguments: [@session, %gregwar_captcha.image.width%, %gregwar_captcha.image.height%]
|
arguments: [@session, %gregwar_captcha.width%, %gregwar_captcha.height%, %gregwar_captcha.length%]
|
||||||
tags:
|
tags:
|
||||||
- { name: form.type, alias: captcha }
|
- { name: form.type, alias: captcha }
|
||||||
|
|
|
@ -33,6 +33,12 @@ class CaptchaType extends AbstractType
|
||||||
*/
|
*/
|
||||||
protected $height;
|
protected $height;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The code length
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
protected $length;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The session
|
* The session
|
||||||
* @var Symfony\Component\HttpFoundation\Session
|
* @var Symfony\Component\HttpFoundation\Session
|
||||||
|
@ -42,12 +48,12 @@ class CaptchaType extends AbstractType
|
||||||
private $key = 'captcha';
|
private $key = 'captcha';
|
||||||
|
|
||||||
|
|
||||||
public function __construct(Session $session, $width, $height)
|
public function __construct(Session $session, $width, $height, $length)
|
||||||
{
|
{
|
||||||
$this->session = $session;
|
$this->session = $session;
|
||||||
$this->width = $width;
|
$this->width = $width;
|
||||||
$this->height = $height;
|
$this->height = $height;
|
||||||
|
$this->length = $length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildForm(FormBuilder $builder, array $options)
|
public function buildForm(FormBuilder $builder, array $options)
|
||||||
|
@ -70,12 +76,18 @@ class CaptchaType extends AbstractType
|
||||||
{
|
{
|
||||||
if (isset($options['width'])) {
|
if (isset($options['width'])) {
|
||||||
$this->width = $options['width'];
|
$this->width = $options['width'];
|
||||||
|
}
|
||||||
|
if (isset($options['height'])) {
|
||||||
$this->height = $options['height'];
|
$this->height = $options['height'];
|
||||||
}
|
}
|
||||||
|
if (isset($options['length'])) {
|
||||||
|
$this->length = $options['length'];
|
||||||
|
}
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'width' => $this->width,
|
'width' => $this->width,
|
||||||
'height' => $this->height
|
'height' => $this->height,
|
||||||
|
'length' => $this->length
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +107,7 @@ class CaptchaType extends AbstractType
|
||||||
$value = '';
|
$value = '';
|
||||||
$chars = str_split($charset);
|
$chars = str_split($charset);
|
||||||
|
|
||||||
for ($i=0; $i<5; $i++) {
|
for ($i=0; $i<$this->length; $i++) {
|
||||||
$value.= $chars[array_rand($chars)];
|
$value.= $chars[array_rand($chars)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue