Category Archives: zend framework

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

Zend Framework 2 – Create New Project From Scratch

I’ve noticed many users are still incapable of getting Zend Framework 2 going on there local machine. I’m going to run a very thorough explanation of how I got it working.

Information and tools used throughout this tutorial:
Zend Framework 2 online tutorial
Windows 7 (64 bit)
EasyPHP DevServer 13.1 (everything that comes with it)

Let’s begin.

Step 1: Download and install EasyPHP DevServer 13.1

(If you installed PHP and MySQL some other way then just follow the instructions and make sure all your settings are identical to get Zend Framework 2 working)

Step 2: Make sure your PHP variable is working correctly. To do so, open Terminal by clicking Start, type “cmd“, click on “cmd.exe“. Once the terminal is open, type “php -v” and press Enter. If setup correctly, you will see PHP version and some other information, if not you will get an error along the lines of “php is not a recognizable command …“.

If PHP variable is working correctly then skip directly to Step 3, otherwise read on.

(EasyPHP should setup this correctly on install, if you used something else to install PHP then perform the following procedures:
– Find and the copy your full path to php.exe, (ex. C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\binaries\php\php_runningversion)
– Open Environment variables settings (Right-click “Computer” icon, click Properties, then click “Advanced System Settings” on the left side panel, then click “Environment Variables” button at the very bottom.)
– In the bottom list, find PATH in the first column and click Edit button.
– Place the pointer to the beginning of the line and paste the previously copied full path to PHP.exe.
– Add a semi-colon “;” after the path)

Step 3: Download Zend Framework 2 skeleton application from here.

Step 4: Inside your working directory (where you keep all your PHP projects, ex. “www”) create a folder “zf2-tutorial” and unzip the contents of Zend Framework 2 downloaded file in there.

In my case (because I’m using EasyPHP), the local working directory is “C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\projects“, so I placed my “zf2-tutorial” folder inside the “projects” folder.

End path where “zf2-tutorial” is placed is: C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\projects\zf2-tutorial

Step 5: Now onto the fun stuff with Zend Framework 2. Open the Terminal again, type the following command to switch inside “zf2-tutorial” folder:

In your case, you place the path to your “zf2-tutorial” folder.

Step 6: Type the following command and press Enter:

Let the process complete.

Step 7: Type the following command and press Enter:

Let this process complete before you proceed. It may take a while. (I got an error here because EasyPHP comes with OpenSSL is disabled and I couldn’t proceed. If you did too, just open php.ini file and un-comment openssl extension.

Step 8: Now let’s add virtual hosts to httpd.conf file. Open httpd.conf file and find (commented out) example of Virtual Hosts setup. Now paste the following below the example but don’t comment it out like the examples:

The only 2 things you have to change is the path to your zf2-tutorial project. First is on the DocumentRoot line and second is inside the first Directory tag. If you are using EasyPHP then just replicate the path to the example I provided.

Step 9: Modify your hosts file. (You won’t be able to save your hosts’ file changes unless you open it in Notepad (or other editor) as Administrator).

Before:

After:

I kind of wish I had a step 10 but I don’t. Your Zend Framework 2 setup should be working flawlessly right now. Let me know if you have any questions and I’ll do my best to try and assist you in your setup.

Cheers.