Merge pull request #1 from centove/master

Form Theme
This commit is contained in:
Grégoire Passault 2011-09-09 10:40:31 -07:00
commit 959cf84805
7 changed files with 155 additions and 46 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace Gregwar\CaptchaBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree.
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('gregwar_captcha', 'array');
$rootNode
->children()
->arrayNode('image')
->addDefaultsIfNotSet()
->children()
->scalarNode('width')->defaultValue(120)->end()
->scalarNode('height')->defaultValue(40)->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}

View File

@ -12,12 +12,19 @@ class GregwarCaptchaExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('gregwar_captcha.image.height', $config['image']['height']);
$container->setParameter('gregwar_captcha.image.width', $config['image']['width']);
$resources = $container->getParameter('twig.form.resources');
$resources[] = 'GregwarCaptchaBundle::captcha.html.twig';
$container->setParameter('twig.form.resources', $resources);
$container->setParameter('twig.form.resources',array_merge(array('GregwarCaptchaBundle::captcha.html.twig'), $resources));
}
}

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));
@ -30,10 +33,10 @@ class CaptchaGenerator {
// Draw random lines
for ($t=0; $t<10; $t++) {
$tcol = imagecolorallocate($i, 100+mt_rand(0,150), 100+mt_rand(0,150), 100+mt_rand(0,150));
$Xa = mt_rand(0, 120);
$Ya = mt_rand(0, 40);
$Xb = mt_rand(0, 120);
$Yb = mt_rand(0, 40);
$Xa = mt_rand(0, $width);
$Ya = mt_rand(0, $height);
$Xb = mt_rand(0, $width);
$Yb = mt_rand(0, $height);
imageline($i, $Xa, $Ya, $Xb, $Yb, $tcol);
}
@ -41,15 +44,15 @@ class CaptchaGenerator {
imagettftext($i, 28, 0, 5, 32, $col, dirname(__FILE__).'/Font/captcha.ttf', $this->value);
// Distort the image
$X = mt_rand(0, 120);
$Y = mt_rand(0, 40);
$X = mt_rand(0, $width);
$Y = mt_rand(0, $height);
$Phase=mt_rand(0,10);
$Scale = 1.3 + mt_rand(0,10000)/30000;
$Amp=1+mt_rand(0,1000)/1000;
$out = imagecreatetruecolor(120,40);
$out = imagecreatetruecolor($width, $height);
for ($x=0; $x<120; $x++)
for ($y=0; $y<40; $y++) {
for ($x=0; $x<$width; $x++)
for ($y=0; $y<$height; $y++) {
$Vx=$x-$X;
$Vy=$y-$Y;
$Vn=sqrt($Vx*$Vx+$Vy*$Vy);
@ -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

@ -1,43 +1,79 @@
Gregwar's CaptchaBundle
=====================
`GregwarCaptchaBundle` provides the form type "captcha"
The `GregwarCaptchaBundle` adds support for a "captcha" form type for the
Symfony2 form component.
Installation
============
To install `GregwarCaptchaBundle`, first adds it to your `deps`:
### Step 1: Download the GregwarCaptchaBundle
Ultimately, the GregwarCaptchaBundle files should be downloaded to the
'vendor/bundles/Gregwar/CaptchaBundle' directory.
You can accomplish this several ways, depending on your personal preference.
The first method is the standard Symfony2 method.
***Using the vendors script***
Add the following lines to your `deps` file:
```
[GregwarCaptchaBundle]
git=git://github.com/Gregwar/CaptchaBundle.git
target=/bundles/Gregwar/CaptchaBundle
And run `php bin/vendors install`. Then add the namespace to your `app/autoload.php`
file:
```php
<?php
...
'Gregwar' => __DIR__.'/../vendor/bundles',
...
```
And registers the bundle in your `app/AppKernel.php`:
Now, run the vendors script to download the bundle:
``` bash
$ php bin/vendors install
```
***Using submodules***
If you prefer instead to use git submodules, then run the following:
``` bash
$ git submodule add git://github.com/Gregwar/CaptchaBundle.git vendor/bundles/Gregwar/CaptchaBundle
$ git submodule update --init
```
### Step 2: Configure the Autoloader
Now you will need to add the `Gregwar` namespace to your autoloader:
``` php
<?php
// app/autoload.php
$loader->registerNamspaces(array(
// ...
'Gregwar' => __DIR__.'/../vendor/bundles',
));
```
### Step 3: Enable the bundle
Finally, enable the bundle in the kernel:
```php
<?php
//...
// app/appKernel.php
public function registerBundles()
{
$bundles = array(
...
// ...
new Gregwar\CaptchaBundle\GregwarCaptchaBundle(),
...
);
...
}
```
Adds the following configuration to your `app/config/config.yml`:
Configuration
=============
Add the following configuration to your `app/config/config.yml`:
gregwar_captcha: ~
@ -59,17 +95,21 @@ with route and subrequests.
Form theming
============
If you want to put the image in an other way, you can form theme `captcha_bundle` (this
is the default behavior) :
The widget support the standard symfony theming, see the [documentation](http://symfony.com/doc/current/book/forms.html#form-theming) for details on how to accomplish this.
The default rendering is:
```html
{% 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) }}
{% spaceless %}
{% endblock %}
```
License
=======
This bundle is under the MIT license. See the complete license in the bundle:
LICENSE
This bundle is under MIT license

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