My_Captcha_Image

View: New views
6 Messages — Rating Filter:   Alert me  

My_Captcha_Image

by Raavi Raaj :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
 
I would like to extend the Image adapter for the Captcha element.
I have done so and placed it in My/Captcha/Image.php
 
My question...
I have no clue how to use my adapter for the captcha.
 
This is how I define and add the element
$captcha = new Zend_Form_Element_Captcha('captcha', array('captcha' => array('captcha' => 'Image', 'wordLen' => 4)));
$this->addElement($captcha);
 
All help is appreciated.
 
-R
 
P.S.
- I have a canventional setup
- All my forms extend My_Form

Re: My_Captcha_Image

by zendlearner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, I am having the same problem and I don't know how to use My/Captcha/Image.php instead of Zend/Captcha/Image.php when I create form element using new Zend_Form_Element_Captcha.

$captchaimage = new My_Captcha_Image();

        $this->addElement('captcha', 'captcha', array(
        'label' => 'Word Verification',
        'description' => 'Please verify that you are human',
        'helper' => null,
        'order' => 4,
          'captcha' => array(
        'captcha' => 'Image',
        'wordLen' => 6,
        'timeout' => 300,
        'font' => 'pics/captcha/DeJaVuSansMono.ttf',
        'fontSize' => 30,
        'imgDir' => 'pics/captcha/img',
        'imgUrl' => 'http://foo/pics/captcha/img'
                         )
                ));



Raavi Raaj wrote:
Hi,

I would like to extend the Image adapter for the Captcha element.
I have done so and placed it in My/Captcha/Image.php

*My question...*
I have no clue how to use my adapter for the captcha.

*This is how I define and add the element*
$captcha = new Zend_Form_Element_Captcha('captcha', array('captcha' =>
array('captcha' => 'Image', 'wordLen' => 4)));
$this->addElement($captcha);

All help is appreciated.

-R

P.S.
- I have a canventional setup
- All my forms extend My_Form

Re: My_Captcha_Image

by Lucas Corbeaux :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not a Zend form experienced user, and I can't answer you totally...
But after looking Zend_Form_Element_Captcha::setCaptcha() method, I
think something like that :

$captchaimage = new My_Captcha_Image();

        $this->addElement('captcha', 'captcha', array(
        'label' => 'Word Verification',
        'description' => 'Please verify that you are human',
        'helper' => null,
        'order' => 4,
          'captcha' => $captchaimage
        ));



Would works, because setCaptcha accept both Zend_Captcha_Adapter
instance and options array.
I can also be totally wrong, but maybe try to give to the 'captcha'
index the value 'My_Captcha_Image' instead of 'Image', if you want your
element to instanciate it.

Hope one of this answer will help...
Lucas

zendlearner a écrit :

> Hi, I am having the same problem and I don't know how to use
> My/Captcha/Image.php instead of Zend/Captcha/Image.php when I create form
> element using new Zend_Form_Element_Captcha.
>
> $captchaimage = new My_Captcha_Image();
>
>         $this->addElement('captcha', 'captcha', array(
>         'label' => 'Word Verification',
>         'description' => 'Please verify that you are human',
>         'helper' => null,
>         'order' => 4,
>           'captcha' => array(
>         'captcha' => 'Image',
>         'wordLen' => 6,
>         'timeout' => 300,
>         'font' => 'pics/captcha/DeJaVuSansMono.ttf',
>         'fontSize' => 30,
>         'imgDir' => 'pics/captcha/img',
>         'imgUrl' => 'http://foo/pics/captcha/img'
> )
> ));
>
>
>
>
> Raavi Raaj wrote:
>  
>> Hi,
>>
>> I would like to extend the Image adapter for the Captcha element.
>> I have done so and placed it in My/Captcha/Image.php
>>
>> *My question...*
>> I have no clue how to use my adapter for the captcha.
>>
>> *This is how I define and add the element*
>> $captcha = new Zend_Form_Element_Captcha('captcha', array('captcha' =>
>> array('captcha' => 'Image', 'wordLen' => 4)));
>> $this->addElement($captcha);
>>
>> All help is appreciated.
>>
>> -R
>>
>> P.S.
>> - I have a canventional setup
>> - All my forms extend My_Form
>>
>>
>>    
>
>  


Re: My_Captcha_Image

by zendlearner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Lucas,

Thanks for your response. Unfortunately, neither 'captcha' => $captchaimage nor 'captcha' => 'My_Captcha_Image' works.  You seem to be on the right track, but I get an exception:

Warning: Exception caught by form: Image CAPTCHA requires font Stack Trace: #0 C:\wamp\www\takefoodout\library\TakeFoodOut\Captcha\Image.php(423): TakeFoodOut_Captcha_Image->_generateImage('c60e6c7b5c85f79...', 'r7v5b7wa') #1 C:\wamp\www\takefoodout\library\Zend\Form\Element\Captcha.php(149): TakeFoodOut_Captcha_Image->generate() #2 C:\wamp\www\takefoodout\library\Zend\Form\Decorator\FormElements.php(100): Zend_Form_Element_Captcha->render() #3 C:\wamp\www\takefoodout\library\Zend\Form.php(2595):
...

Here is my code:
<?php
class TakeFoodOut_SignupForm extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        // add elements
       
        $this->addElement('captcha', 'captcha', array(
        'label' => 'Word Verification',
        'description' => 'Please verify that you are human',
        'helper' => null,
        'order'  => 4,
          'captcha' => array(
        'captcha' => 'Image',
        'wordLen' => 6,
        'timeout' => 300,
        'font' => 'pics/captcha/DeJaVuSansMono.ttf',
        'fontSize' => 30,
        'imgDir' => 'pics/captcha/img',
        'imgUrl' => 'http://takefoodout/pics/captcha/img'
                         )
                ));
               
                $this->getElement('captcha')
                        ->addPrefixPath('TakeFoodOut_Captcha', 'TakeFoodOut/Captcha/', 'captcha')
                        ->setCaptcha(new TakeFoodOut_Captcha_Image());


Lucas Corbeaux wrote:
I'm not a Zend form experienced user, and I can't answer you totally...
But after looking Zend_Form_Element_Captcha::setCaptcha() method, I
think something like that :

$captchaimage = new My_Captcha_Image();

        $this->addElement('captcha', 'captcha', array(
        'label' => 'Word Verification',
        'description' => 'Please verify that you are human',
        'helper' => null,
        'order' => 4,
          'captcha' => $captchaimage
        ));



Would works, because setCaptcha accept both Zend_Captcha_Adapter
instance and options array.
I can also be totally wrong, but maybe try to give to the 'captcha'
index the value 'My_Captcha_Image' instead of 'Image', if you want your
element to instanciate it.

Hope one of this answer will help...
Lucas

zendlearner a écrit :
> Hi, I am having the same problem and I don't know how to use
> My/Captcha/Image.php instead of Zend/Captcha/Image.php when I create form
> element using new Zend_Form_Element_Captcha.
>
> $captchaimage = new My_Captcha_Image();
>
>         $this->addElement('captcha', 'captcha', array(
>         'label' => 'Word Verification',
>         'description' => 'Please verify that you are human',
>         'helper' => null,
>         'order' => 4,
>           'captcha' => array(
>         'captcha' => 'Image',
>         'wordLen' => 6,
>         'timeout' => 300,
>         'font' => 'pics/captcha/DeJaVuSansMono.ttf',
>         'fontSize' => 30,
>         'imgDir' => 'pics/captcha/img',
>         'imgUrl' => 'http://foo/pics/captcha/img'
> )
> ));
>
>
>
>
> Raavi Raaj wrote:
>  
>> Hi,
>>
>> I would like to extend the Image adapter for the Captcha element.
>> I have done so and placed it in My/Captcha/Image.php
>>
>> *My question...*
>> I have no clue how to use my adapter for the captcha.
>>
>> *This is how I define and add the element*
>> $captcha = new Zend_Form_Element_Captcha('captcha', array('captcha' =>
>> array('captcha' => 'Image', 'wordLen' => 4)));
>> $this->addElement($captcha);
>>
>> All help is appreciated.
>>
>> -R
>>
>> P.S.
>> - I have a canventional setup
>> - All my forms extend My_Form
>>
>>
>>    
>
>  

Re: My_Captcha_Image

by weierophinney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- zendlearner <kimbaudi@...> wrote
(on Saturday, 21 March 2009, 01:26 PM -0700):
> Thanks for your response. Unfortunately, neither 'captcha' => $captchaimage
> nor 'captcha' => 'My_Captcha_Image' works.  You seem to be on the right
> track, but I get an exception:
>
> Warning: Exception caught by form: Image CAPTCHA requires font Stack Trace:

This is telling you exactly the problem -- you either didn't specify the
font to use, or specified an invalid path. Looking further...

<snip>
>         'font' => 'pics/captcha/DeJaVuSansMono.ttf',

... the problem is you are using a relative path to the font... but
you're not saying where it's relative _to_. Specify a full path to the
font, based on some known path (document root, APPLICATION_PATH constant
if you've set it, dirname(__FILE__) from the file where you create the
form, whatever), and you should be fine.


--
Matthew Weier O'Phinney
Software Architect | matthew@...
Zend Framework     | http://framework.zend.com/

Re: My_Captcha_Image

by zendlearner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

*SOLVED* and found a bug or strange behavior

Hi Matthew, the problem was not the use of a relative path to the font, but the fact that I needed to pass an array of options to my custom captcha image adapter. Here is what worked for me based on my previous code:

<?php
class TakeFoodOut_SignupForm extends Zend_Form
{
    public function init()
    {
        // add the captcha element
        $this->addElement('captcha', 'captcha', array(
        'label' => 'Word Verification',
        'description' => 'Please verify that you are human',
        'helper' => null,
        'order' => 4,
          'captcha' => array(
        'captcha' => 'Image',
        'wordLen' => 6,
        'timeout' => 300,
        'font' => 'pics/captcha/DeJaVuSansMono.ttf',
        'fontSize' => 30,
        'imgDir' => 'pics/captcha/img',
        'imgUrl' => 'http://takefoodout/pics/captcha/img'
                         )
                ));
                $this->getElement('captcha')
                         ->setCaptcha(new TakeFoodOut_Captcha_Image(array(
                          'captcha'=>'Image',
                          'wordLen' => 6,
        'timeout' => 300,
        'font' => 'pics/captcha/DeJaVuSansMono.ttf',
        'fontSize' => 30,
        'imgDir' => 'pics/captcha/img',
        'imgUrl' => 'http://takefoodout/pics/captcha/img'
        )));
    }
}

Note how the instance of TakeFoodOut_Captcha_Image is passed an array of options including the font. I think what confused me was that I was already passing an array of options to addElement() so I didn't think that I needed to do it again in setConfig().

Now I would like to share my preferred way of creating a captcha element using a custom captcha image adapter and addPrefixPath(). I would also like to explain a potential bug when you use this method and how to avoid the bug.  Please take a look at two versions of my code. The commented code is just another way of writing the code above it. The first one causes a bug, and the second one works fine.

Buggy:
<?php
class TakeFoodOut_SignupForm extends Zend_Form
{
    public function init()
    {
        $this->addElement(new Zend_Form_Element_Captcha('captcha', array('captcha'=>'Image')));
        // OR $this->addElement('captcha', 'captcha', array('captcha' => 'Image'));
        // OR $this->addElement('captcha', 'captcha', array('captcha'=> array('captcha' => 'Image')));
       
                $this->getElement('captcha')
                        ->addPrefixPath('TakeFoodOut_Captcha', 'TakeFoodOut/Captcha/', 'captcha')
                        ->setOptions(array(
                                'label' => 'Word Verification',
                                'description' => 'Please verify that you are human',
                                'helper' => null,
                                'order' => 4
                                ))
                        ->setCaptcha('Image', array('font'=>'images/font/DeJaVuSansMono.ttf'));
                /* OR
                $this->getElement('captcha')
                        ->addPrefixPath('TakeFoodOut_Captcha', 'TakeFoodOut/Captcha/', 'captcha')
                        ->setOptions(array(
                                'label' => 'Word Verification',
                                'description' => 'Please verify that you are human',
                                'helper' => null,
                                'order' => 4,
        'captcha' => array(
                          'captcha'=>'Image',
                          'wordLen' => 6,
        'timeout' => 300,
        'font' => 'pics/captcha/DeJaVuSansMono.ttf',
        'fontSize' => 30,
        'imgDir' => 'pics/captcha/img',
        'imgUrl' => 'http://takefoodout/pics/captcha/img'
        )
                                ));
                */
    }
}

Works fine:
<?php
class TakeFoodOut_SignupForm extends Zend_Form
{
    public function init()
    {
        $this->addElement(new Zend_Form_Element_Captcha('captcha', array('captcha'=>'Image')));
        // OR $this->addElement('captcha', 'captcha', array('captcha' => 'Image'));
        // OR $this->addElement('captcha', 'captcha', array('captcha'=> array('captcha' => 'Image')));
       
                $this->getElement('captcha')
                        ->addPrefixPath('TakeFoodOut_Captcha', 'TakeFoodOut/Captcha/', 'captcha')
                        ->setOptions(array(
                                'label' => 'Word Verification',
                                'description' => 'Please verify that you are human',
                                'helper' => null,
                                'order' => 4
                                ))
                        ->setCaptcha('Foo', array('font'=>'images/font/DeJaVuSansMono.ttf'));
                /* OR
                $this->getElement('captcha')
                        ->addPrefixPath('TakeFoodOut_Captcha', 'TakeFoodOut/Captcha/', 'captcha')
                        ->setOptions(array(
                                'label' => 'Word Verification',
                                'description' => 'Please verify that you are human',
                                'helper' => null,
                                'order' => 4,
        'captcha' => array(
                          'captcha'=>'Foo',
                          'wordLen' => 6,
        'timeout' => 300,
        'font' => 'pics/captcha/DeJaVuSansMono.ttf',
        'fontSize' => 30,
        'imgDir' => 'pics/captcha/img',
        'imgUrl' => 'http://takefoodout/pics/captcha/img'
        )
                                ));
                */
    }
}

Notice the difference between the two codes? They are exactly the same except for setConfig() method:

Buggy: setCaptcha('Image', array('font'=>'images/font/DeJaVuSansMono.ttf'));
Works fine: setCaptcha('Foo', array('font'=>'images/font/DeJaVuSansMono.ttf'));

Basically, setCaptcha('Image') calls Zend_Captcha_Image instead of TakeFoodOut_Captcha_Image, even though I added prefix path: addPrefixPath('TakeFoodOut_Captcha', 'TakeFoodOut/Captcha/', 'captcha'). To prevent this from happening, I had to rename class TakeFoodOut_Captcha_Image to class TakeFoodOut_Captcha_Foo and rename TakeFoodOut/Captcha/Image.php to  TakeFoodOut/Captcha/Foo.php. Once I made the change, it now works.

I hope this helps someone in their attempts to customize the captcha form element.  Prior to this, I thought that I can customize the captcha form element using just form decorators. However, I now realize that there are times when form decorators are insufficient and the only way is to create custom captcha adapters.






Matthew Weier O'Phinney-3 wrote:
-- zendlearner <kimbaudi@gmail.com> wrote
(on Saturday, 21 March 2009, 01:26 PM -0700):
> Thanks for your response. Unfortunately, neither 'captcha' => $captchaimage
> nor 'captcha' => 'My_Captcha_Image' works.  You seem to be on the right
> track, but I get an exception:
>
> Warning: Exception caught by form: Image CAPTCHA requires font Stack Trace:

This is telling you exactly the problem -- you either didn't specify the
font to use, or specified an invalid path. Looking further...

<snip>
>         'font' => 'pics/captcha/DeJaVuSansMono.ttf',

... the problem is you are using a relative path to the font... but
you're not saying where it's relative _to_. Specify a full path to the
font, based on some known path (document root, APPLICATION_PATH constant
if you've set it, dirname(__FILE__) from the file where you create the
form, whatever), and you should be fine.


--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework     | http://framework.zend.com/