Velox Letum
12-02-2005, 06:20 AM
I quickly wrote up a little class to do GD Image Validation, as I've seen a few people asking about it. GD Image Validation is useful for forms where you don't want automated submissions, such as email contact forms or registration forms. The class generates a random value and outputs it as an image, also setting it to a session variable to be compared with the user-entered data. I've posted the class, and the example below.
<?php
// Velox Letum (2005)
// elementation@gmail.com
class gd_verification {
var $im = NULL;
var $string = NULL;
var $height;
var $width;
function gd_verification ($height = 150, $width = 35, $sid = NULL) {
if ($sid != NULL) {
session_name($sid);
}
// Set image height/width
$this->height = $height;
$this->width = $width;
// Start session
session_start();
}
function generate_string () {
// Create random string
$this->string = substr(sha1(mt_rand()), 17, 6);
// Set session variable
$_SESSION['gd_string'] = $this->string;
}
function verify_string ($gd_string) {
// Check if the original string and the passed string match...
if ($_SESSION['gd_string'] === $gd_string) {
return TRUE;
} else {
return FALSE;
}
}
function output_input_box ($name, $parameters = NULL) {
return '<input type="text" name="' . $name . '" ' . $parameters . ' /> ';
}
function create_image () {
// Seed string
$this->generate_string();
$this->im = imagecreatetruecolor($this->height, $this->width); // Create image
// Get width and height
$img_width = imagesx($this->im);
$img_height = imagesy($this->im);
// Define some common colors
$black = imagecolorallocate($this->im, 0, 0, 0);
$white = imagecolorallocate($this->im, 255, 255, 255);
$red = imagecolorallocatealpha($this->im, 255, 0, 0, 75);
$green = imagecolorallocatealpha($this->im, 0, 255, 0, 75);
$blue = imagecolorallocatealpha($this->im, 0, 0, 255, 75);
// Background
imagefilledrectangle($this->im, 0, 0, $img_width, $img_height, $white);
// Ellipses (helps prevent optical character recognition)
imagefilledellipse($this->im, ceil(rand(5,$img_width - 5)), ceil(rand(0,$img_height)), 30, 30, $red);
imagefilledellipse($this->im, ceil(rand(5,$img_width - 5)), ceil(rand(0,$img_height)), 30, 30, $green);
imagefilledellipse($this->im, ceil(rand(5,$img_width - 5)), ceil(rand(0,$img_height)), 30, 30, $blue);
// Borders
imagefilledrectangle($this->im, 0, 0, $img_width, 0, $black);
imagefilledrectangle($this->im, $img_width - 1, 0, $img_width - 1, $img_height - 1, $black);
imagefilledrectangle($this->im, 0, 0, 0, $img_height - 1, $black);
imagefilledrectangle($this->im, 0, $img_height - 1, $img_width, $img_height - 1, $black);
imagestring ($this->im, 5, intval(($img_width - (strlen($this->string) * 9)) / 2), intval(($img_height - 15) / 2), $this->string, $black); // Write string to photo
}
function output_image() {
$this->create_image(); // Generate image
header("Content-type: image/jpeg"); // Tell the browser the data is a JPEG image
imagejpeg($this->im); // Output Image
imagedestroy($this->im); // Flush Image
}
}
?>
Then to use it I made an example (two files), the form:
<?php
// Velox Letum (2005)
// elementation@gmail.com
// Require the class code...
require ('gd_image_verify.php');
// Initialize class
$gd = new gd_verification();
if (isset($_POST['submit'])) {
if ($gd->verify_string($_POST['gd_string'])) {
$result = 'User input matches image string.';
} else {
$result = 'User input does not match image string.';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>GD Image Verification Class Example</title>
<head>
<body>
<div style="text-align:center;">
<h1>GD Image Verification Class Example</h1>
<? echo $result; ?><br /><br />
<form name="gd_verify" method="POST" action="<? echo $PHP_SELF; ?>">
<img src="gd_image.php" alt="Verification Image" /><br /><br />
<? echo $gd->output_input_box('gd_string'); ?><br /><br .>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
</body>
</html>
and the image output:
<?php
// Velox Letum (2005)
// elementation@gmail.com
// Require the class code...
require ('gd_image_verify.php');
// Initialize class
$gd = new gd_verification();
// Output image
$gd->output_image();
?>
Enjoy.
<?php
// Velox Letum (2005)
// elementation@gmail.com
class gd_verification {
var $im = NULL;
var $string = NULL;
var $height;
var $width;
function gd_verification ($height = 150, $width = 35, $sid = NULL) {
if ($sid != NULL) {
session_name($sid);
}
// Set image height/width
$this->height = $height;
$this->width = $width;
// Start session
session_start();
}
function generate_string () {
// Create random string
$this->string = substr(sha1(mt_rand()), 17, 6);
// Set session variable
$_SESSION['gd_string'] = $this->string;
}
function verify_string ($gd_string) {
// Check if the original string and the passed string match...
if ($_SESSION['gd_string'] === $gd_string) {
return TRUE;
} else {
return FALSE;
}
}
function output_input_box ($name, $parameters = NULL) {
return '<input type="text" name="' . $name . '" ' . $parameters . ' /> ';
}
function create_image () {
// Seed string
$this->generate_string();
$this->im = imagecreatetruecolor($this->height, $this->width); // Create image
// Get width and height
$img_width = imagesx($this->im);
$img_height = imagesy($this->im);
// Define some common colors
$black = imagecolorallocate($this->im, 0, 0, 0);
$white = imagecolorallocate($this->im, 255, 255, 255);
$red = imagecolorallocatealpha($this->im, 255, 0, 0, 75);
$green = imagecolorallocatealpha($this->im, 0, 255, 0, 75);
$blue = imagecolorallocatealpha($this->im, 0, 0, 255, 75);
// Background
imagefilledrectangle($this->im, 0, 0, $img_width, $img_height, $white);
// Ellipses (helps prevent optical character recognition)
imagefilledellipse($this->im, ceil(rand(5,$img_width - 5)), ceil(rand(0,$img_height)), 30, 30, $red);
imagefilledellipse($this->im, ceil(rand(5,$img_width - 5)), ceil(rand(0,$img_height)), 30, 30, $green);
imagefilledellipse($this->im, ceil(rand(5,$img_width - 5)), ceil(rand(0,$img_height)), 30, 30, $blue);
// Borders
imagefilledrectangle($this->im, 0, 0, $img_width, 0, $black);
imagefilledrectangle($this->im, $img_width - 1, 0, $img_width - 1, $img_height - 1, $black);
imagefilledrectangle($this->im, 0, 0, 0, $img_height - 1, $black);
imagefilledrectangle($this->im, 0, $img_height - 1, $img_width, $img_height - 1, $black);
imagestring ($this->im, 5, intval(($img_width - (strlen($this->string) * 9)) / 2), intval(($img_height - 15) / 2), $this->string, $black); // Write string to photo
}
function output_image() {
$this->create_image(); // Generate image
header("Content-type: image/jpeg"); // Tell the browser the data is a JPEG image
imagejpeg($this->im); // Output Image
imagedestroy($this->im); // Flush Image
}
}
?>
Then to use it I made an example (two files), the form:
<?php
// Velox Letum (2005)
// elementation@gmail.com
// Require the class code...
require ('gd_image_verify.php');
// Initialize class
$gd = new gd_verification();
if (isset($_POST['submit'])) {
if ($gd->verify_string($_POST['gd_string'])) {
$result = 'User input matches image string.';
} else {
$result = 'User input does not match image string.';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>GD Image Verification Class Example</title>
<head>
<body>
<div style="text-align:center;">
<h1>GD Image Verification Class Example</h1>
<? echo $result; ?><br /><br />
<form name="gd_verify" method="POST" action="<? echo $PHP_SELF; ?>">
<img src="gd_image.php" alt="Verification Image" /><br /><br />
<? echo $gd->output_input_box('gd_string'); ?><br /><br .>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
</body>
</html>
and the image output:
<?php
// Velox Letum (2005)
// elementation@gmail.com
// Require the class code...
require ('gd_image_verify.php');
// Initialize class
$gd = new gd_verification();
// Output image
$gd->output_image();
?>
Enjoy.