Creating a PHP Verification Image

10:26 PM Mar 22nd 2007 from web under ,

You will need to have GD library loaded on your server, and you should be using PHP4+. To check to see if you have either of these running, simply create a file, call it whatever you want, I call mine info.php. Place the following code in there:

1
<? phpinfo(); ?>

If all checks out, then you are ready to move on.

Firstly, you will need to create a session, both in your image script, and the page that will be serving the image, you will see why later.

1
session_start();

And for some, you will have to create a save path for your sessions.

1
session_save_path('path/to/tmp');

Once you have set this up, you can now start creating your image. Lets start with the variables needed.

1
2
3
4
5
6
$w = 120;
$h = 28;
$x = 6;
$y = 20;
$font = "fonts/arial.ttf";
$font_size = 16;

These variables can be changed to your liking, I chose the basics to get you started. But, for example, you can change arial.ttf to whatever .ttf you like, as long as it is not wingdings, you should be all set.
Next we create the image, and set its background color and text color.

1
2
3
$image = imagecreate($w,$h);
$background_color = imagecolorallocate($image,215,255,255);
$text_color = imagecolorallocate($image,90,90,90);

Now we create our random string for which we set our text to the image for display, and set our SESSION variable.

1
2
3
$str = md5(rand(0,9999));
$output = substr($str, 14, 6);
$_SESSION['verify'] = $output;

With that complete, we move on to applying the random string to the blank image we created earlier.

1
2
3
4
5
6
7
8
9
10
11
if(file_exists($font)) {
putenv('GDFONTPATH=' . realpath('.'));
for($i = 0; $i<strlen($output); $i++) {
$ny = rand(200,245)/10;
$angle = rand(-22,19);
imagettftext($image, $font_size, $angle, $x, $ny, $text_color, $font, $output[$i]);
$x += rand(16,18);
}
} else {
die('Font file does not exist.');
}

Now our image has been created, and our SESSION variable has been set, we are almost finished. All that is left is to set our headers, and send the image off.

1
2
3
4
5
6
7
8
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

We do not ever want to cache our images, and we always want to destroy them after we send them.

Now, in whatever page you choose to have this verification work, you simply need to add a session_start(); and in an image tag, point the source to this php file.

1
<img src="image_verification.php" title="Verify" />

You should be able to figure out the rest when it comes to comparing the user input with the SESSION variable.

Reader Comments

Show all comments
  1. Dummies says:

    Have a problem with for loop

    for($i = 0; $i < strlen($output); $i++) {

    Bug happens there, i tried and returned the

    Parse error: syntax error, unexpected ‘;’, expecting ‘)’ in D:\xampp\htdocs\aboutmetk\verificationimages\verification.php on line 29

    Can you help me?

  2. Fonts Guru says:

    This is what i was looking for.

    I knew you could create images, just didn’t realize you could call them like

    i was using frames as i kept getting a “header already called error”

    Thanks

  3. stifler says:

    you can do it without the save path too you know. i couldnt get this to work with the save path on, so i took it out, and it worked.

Leave a Reply