Not so useless! image_to_text() as a CAPTCHA
This entry is guest-posted by CodeIgniter programmer Alexander "Iksander" Springmeyer, who contacted me about a unique use of my "Most Useless CodeIgniter Helper Ever". I've asked him to write up a few words, that I present here for all. My sincerest thanks to go Alexander for looking for new ways to implement old ideas, and for teaching an the old image2text() dog a new trick!
-Derek
Some time ago I happened across Derek's blog post on the 'useless' image2text helper he had created. Despite all the "it is useless" comments in the entry, I felt it had powerful possibilities offered by its application in my Sentinel re-write and upgrade of the Freak Auth light authentication system (by Daniel "danfreak" Vecchiato). [editors note: if you want to, you can read a very long and detailed account of its evolution.]
I was inspired to use it for a CATPCHA. Here's how did I do it?
Well, first, the base system: the CAPTCHA is generated with a base image of 130 x 51 pxs flat color, a randomly generated string, mixed with integers and characters at 5 chars max length. Which is then 'watermarked' onto the base image. This so far is your most basic CAPTCHA; no distortion, just a random string generated as an image.
Then I applied image2text. In the view form that uses the CAPTCHA, instead of this code:
<img src="tmp/1231215566733.12323.jpg" alt="CAPTCHA" />
I altered it to this:
<p style="font-size: 6px; line-height: 30%; letter-spacing: -3px; word-spacing: -3px;">
<?=image_to_text('tmp/1232455235.314.jpg', $captchaString);?>
</p>
Note: I have the generating function return the generated file name, but, for simplicity I just used a static one in this article.
The variable $captchaString is an array key (set in the controller) that is given a random string value to be passed into the image_to_text() function. This generates a body of text which is contained within a <p></p> (notice the CSS!) element that uses in-line styling to achieve the wonderfully high resolution. Without the CSS styles, the generated text-image took up half of my screen, which is running at 1280 x 1012.
Remember, this is NOT an image anymore - it is just a bunch of random strings with the characters colored in an ordered fashion as to RESEMBLE the originally generated image - this would be very difficult for a traditional OCR reading algorithm to interpret, because there is no image. On top of that, the string used to resemble the image is completely different from the actual characters being recreated (even though it could be a static word without harm).
Another point to think about: usability - this would drive screen readers NUTS. So it might be wise to include some sort of a warning (before the containing element) to ensure any user using a screen reader knows what they are about to encounter.
The execution time as profiled by Code Igniter's default output profiler, was at about 0.19 seconds and the original image being generated and used as CAPTCHA (instead of image2text) executed at about 0.09 seconds. So, as you can see - keep the image simple, reasonably sized and you have a powerful CAPTCHA system that isn't killing your CPU cycles.
Note: these tests have only been conducted on my local machine which is running dual XEON processors at 2.6 ghz each - a dedicated server would have no problem, but shared hosting is notorious for CPU limits - so, PROFILE this system on your host before going live just to be safe.
Here are two screenshots - one of the original CAPTCHA image.
And another of the image2text plugin.
Original code used to generate CAPTCHA images was built by Daniel "danfreak" Vecchiato using the Code Igniter image_lib and his own random character generating function. Thanks go-to Derek Allard for a "use-FUL" CodeIgniter helper!!
-- Alexander "Iksander" Springmeyer
Alexander Springmeyer is a 20 year old self-taught programmer (looking for opportunity!), living in Las Vegas, Nevada USA. He's a philosopher, meta-physician, Logician, world traveler, and active athlete. In love with CodeIgniter - Alexander seems glued to the computer screen these days in light of all the exciting activity CodeIgniter has seen of late. Check out his site at orchadea.com.

Derek wrote on
Alexander, this was so great - what an incredible show of creativity. I’m always excited when you see new things rise out from old ideas, and this was executed wonderfully.
I was especially glad to see that you took the time to profile it - when I originally wrote the plugin even I commented on what a pig it was, but your benchmarks seem to show that it isn’t ultra-fast, it could certainly be acceptable.
I don’t think we’ve solved the “security problem”, but this is a wonderful show of programming skill and creativity. Thanks!