From 421e1827c099143856ddef5c24b8e8299c75be6a Mon Sep 17 00:00:00 2001 From: Gregory McLean Date: Thu, 8 Sep 2011 09:12:30 -0400 Subject: [PATCH 1/5] Clarify instructions and fix some typos. --- README.md | 69 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 67b0534..8cd9c99 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,75 @@ 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 - __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 +registerNamspaces(array( + // ... + 'Gregwar' => __DIR__.'/../vendor/bundles', +)); +``` +### Step 3: Enable the bundle + +Finally, enable the bundle in the kernel: ```php Date: Thu, 8 Sep 2011 11:02:40 -0400 Subject: [PATCH 2/5] Add a width and height configuration parameter and fix the form theming so that it works. --- DependencyInjection/Configuration.php | 33 +++++++++++++++++++ .../GregwarCaptchaExtension.php | 11 +++++-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 DependencyInjection/Configuration.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..5b4324b --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,33 @@ +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; + } +} + diff --git a/DependencyInjection/GregwarCaptchaExtension.php b/DependencyInjection/GregwarCaptchaExtension.php index 5292533..d1769c4 100755 --- a/DependencyInjection/GregwarCaptchaExtension.php +++ b/DependencyInjection/GregwarCaptchaExtension.php @@ -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)); + } + } From e2a705dda79e2cf41b7310d3068f05d49af3f5e5 Mon Sep 17 00:00:00 2001 From: Gregory McLean Date: Thu, 8 Sep 2011 11:07:04 -0400 Subject: [PATCH 3/5] Added captcha image width and height as a configuration setting. --- Generator/CaptchaGenerator.php | 17 +++++++++------- Resources/config/services.yml | 2 +- Resources/views/captcha.html.twig | 5 ++++- Type/CaptchaType.php | 33 ++++++++++++++++++++++++++----- 4 files changed, 43 insertions(+), 14 deletions(-) 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) From 2ccb30cd8dea8d0268638509e220432897eb7794 Mon Sep 17 00:00:00 2001 From: Gregory McLean Date: Fri, 9 Sep 2011 08:28:13 -0400 Subject: [PATCH 4/5] Further updates. --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8cd9c99..2741e3e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Ultimately, the GregwarCaptchaBundle files should be downloaded to the You can accomplish this several ways, depending on your personal preference. The first method is the standard Symfony2 method. -**Using the vendors script ** +***Using the vendors script*** Add the following lines to your `deps` file: @@ -32,6 +32,7 @@ $ php bin/vendors install ``` ***Using submodules*** + If you prefer instead to use git submodules, then run the following: ``` bash @@ -69,6 +70,9 @@ public function registerBundles() } ``` +Configuration +============= + Add the following configuration to your `app/config/config.yml`: gregwar_captcha: ~ @@ -91,13 +95,16 @@ 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 %} - +{% spaceless %} + {{ form_widget(form) }} +{% spaceless %} {% endblock %} ``` From 878ff4d33f091a88d83bdba4b573cc36f969e45b Mon Sep 17 00:00:00 2001 From: Gregory McLean Date: Fri, 9 Sep 2011 09:57:05 -0400 Subject: [PATCH 5/5] Catch some more references to the hard coded size and change to the variable one. --- Generator/CaptchaGenerator.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Generator/CaptchaGenerator.php b/Generator/CaptchaGenerator.php index 82247e5..2981c45 100644 --- a/Generator/CaptchaGenerator.php +++ b/Generator/CaptchaGenerator.php @@ -33,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); } @@ -44,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);