Tag Archives: native captcha

Customizing Magento’s Native Captcha (Advanced)

If you implemented native Magento captcha before (potentially using this tutorial Magento Captcha Contact Form) you’ll notice that captcha image is fairly small in height, has a lot of dots (also knows as noise level), some random lines, and very small font.

native captcha on contact form

Luckily, all these attributes (as well as background) can be changed. Magento has a block class Mage_Captcha_Model_Zend located in “app/code/core/Mage/Captcha/Model/Zend.php” which extends Zend_Captcha_Image class.

Zend_Captcha_Image class contains functions we need to modify properties like noise level, lines, and font height. We’re not going to touch this class because the class which renders our captcha using _toHtml() function is Mage_Captcha_Model_Zend.

Note: This example is a continuation (or builds on top of) primary tutorial on setting up native captcha on contact page.

Overview: We will extend Mage_Captcha_Model_Zend model, make necessary changes to captcha settings, and modify our config.xml to indicate to Magento that we’re rewriting Magento’s core file with our’s.

Step 1: Create a new block class inside “app/code/local/Tutorial/Captcha/Block/Captcha/” called Zend.php, and paste the following code.

Yellow lines outline new addition on top of core _toHtml() function found inside Mage_Captcha_Model_Zend. Functions used in this piece of code are self-explanatory so I’ll just list default values Magento uses when generating native captcha.

In our new _toHtml() function we reduce noise level from 200 to 50, set line noise level from 5 to 0, and increase font-size from 24 to 30.

Step 2: Modify config.xml to ensure Magento uses our Block class to generate new captcha image. Below is the complete config.xml used in initial tutorial with additions of new block override code highlight in yellow.

In this snippet we override Mage_Captcha_Model_Zend class with our own Tutorial_Captcha_Block_Captcha_Zend. In result, you get the following captcha which is far more readable then native captcha generation. Keep in mind, captcha is meant to be hard to read but not impossible. How readable you make captcha is up to your and your requirements, this is just an example and is not used in practice.

magento modified captcha image

This is it for this example but not the end to allowed modifications. Further, you can modify captcha image background, font style used to generate text, font text color and more. This can be achieved by extending and overriding “_generateImage($id, $word)” function inside Zend’s Zend_Captcha_Image class.

Update: GitHub repository now available