Archive for the ‘programming’ Category

Creating a PHP Verification Image

Thursday, March 22nd, 2007

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.

Amazing 3D Flash Development

Saturday, July 22nd, 2006

I have recently become fascinated with 3d in the Flash environment, not to experiment, just merely observing. This is due in part by an ex-colleague of mine, Todd Anderson, who has been experimenting with game development and 3d for quite some time now. So as a naturally curious person, I decide to venture out in the the wild world of the web and search out other game and 3d developers in the flash environment. This article is a dedication to one blog I recently came across, which has amazing articles on 3d and physics development in actionscript. I strongly urge you to check the blog out and bookmark it for future reference if you are at all slightly interested in this kind of stuff. Check out Michael Baczynski’s blog now!