I’m sure you’ve seen those images that display a changing value in them, something that you have to read and then type into a field so that you verify that you’re human (the assumption being that a machine couldn’t read the image to extract the text). They’re everywhere.
Do you need one? That’s a question I can’t answer for you. But I can tell you how to create the image. I am working on another bit to tell you how to actually use the image for verification, but since I can’t quite get it working myself, I’m going to leave that part out. This just shows you how to create the image itself.
First and foremost, you need PHP. Without it, you can’t use these instructions. That isn’t to say that there isn’t another method using Perl or another language – but these are specific to PHP. Here’s how to do it.
<?php
$img = ImageCreate(40, 20);
Declare your PHP code, then define the image. The first number is the width of the image and the second number is the height of the image.
$white = ImageColorAllocate($img, 255, 255, 255);
$brown = ImageColorAllocate($img, 51, 0, 0);
Create some variables to define colors. I use white and brown, as that’s the color scheme that I want. The first value in the function is the image that you defined previously, then you’ve got your red, green and blue values, just like on a web page. Only difference is that you’re using the decimal values of those colors, not hex.
srand((double)microtime()*1000000);
$string = md5(rand(0,9999));
$verify_string = substr($string, 3, 4);
These functions seed your random number, run the random number through MD5, and finally create the verification string. The result of the MD5 will be a 32-digit number, so you probably want to cut that size down. Here, I’m using the substr function to get 4 characters, starting at character 3 (remember this position starts with 0, so it’s actually the fourth character of the string). Change those numbers if you’d like to pull a different portion of the string or a longer piece of the string.
ImageFill($img, 0, 0, $brown);
ImageString($img, 3, 6, 3, $verify_string, $white);
Now we color in the image. First, we fill with the background color ($brown in this case). First parameter is the image ($img), then the starting location (top, left) and finally the color to use. Next, we write out our verification string. First parameter to this function is the image ($img), then we specify the font (1-5), the position (top, left), the string to write ($verify_string) and finally the color ($white).
ImagePNG($img, “/full/path/to/verify.png”);
ImageDestroy($img);
?>
Finally, we write the file. Specify the image ($img) and the path to the file (include site path if needed – not URL). Then we destroy the image in memory so that it doesn’t take up space. And then we close our PHP fragment. Now you can pull in the file (verify.png in this case) for display on your page. Here’s what it looks like:
Most, if not all, of the code contained within this entry actually came from a tutorial that I found on another site. While I’ve certainly changed the values and some names to make things work a little more as I’d like them, I wasn’t the one who created this stuff.
Comments
2 responses to “Verification Image with PHP”
The point of this is to keep tools from automating it. However, some clever programs can read images.
Is there a utility that does the reverse? Tells you the text from the image? I need to test a website that uses these verification images, and I have to automate the entry of the text.