Struggling to get Zend_Captcha work [solved]

I tried to generate a form with a captcha element (using Zend_Form_Element_Captcha), a Captcha_Image to be more precised.

I ran into a lot of errors:

  1. when outputing the element, following error occured: Fatal error: Method Zend_Form_Element_Captcha::__toString() must not throw an exception
    solution: instead of directly outputing the element, output $captcha->render()
  2. now exception is readable and as follow: exception ‘Zend_Captcha_Exception’ with message ‘Image CAPTCHA requires font’
    now we know this is a font problem. After reading the doc, it’s written that a font path is mandatory. Let’s define one using ‘font’ option upon instanciation and making it point to a font that’s on your system (in my case, I made it point to ‘/usr/share/fonts/truetype/freefont/FreeSans.ttf’)
  3. new exception: exception ‘ErrorException’ with message ‘imagepng() [<a href='function.imagepng'>function.imagepng</a>]: Unable to open ‘./images/captcha/de9073fe85a4c9f16370f6800f5630aa.png’ for writing: No such file or directory’
    After reading a bit more online documentation, Zend_Captcha uses ‘./images/captcha’ as default directory to write generated captcha images => you need to create this folder or call $captcha->setImgDir() to define another directory on which to write generated captcha images.
    Don’t forget to grant write rights on this folder to your server user, usually www-data: “sudo chown www-data:www-data ./images/captcha”
    NOTE: ‘./images/captcha’ path is not relative to bootstrap file as stated into documentation, but relative to ‘/application/public’ folder (so your full path is ‘/application/public/images/captcha’)
  4. F5 => Now it worked!

sources

Leave a Reply