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)); + } + } diff --git a/Generator/CaptchaGenerator.php b/Generator/CaptchaGenerator.php index c923497..2981c45 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)); @@ -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, diff --git a/README.md b/README.md index 67b0534..2741e3e 100644 --- a/README.md +++ b/README.md @@ -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 - __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 +{% spaceless %} + {{ 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 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)