Tag Archives: gd

A Quick Captcha Plugin

Sometime s the fastest and the best Code snippets are “home-made” :-)

Below is a Captcha Snippet coded for a quick project, Use share

<?php
session_start();
/*

Quick Captcha Plugin by GlobalInteractiveSystems.com

Note:  Not tested, Use at your own risk.

*/

 

function create_image()
{
//Let’s generate a totally random string using md5
$md5_hash = md5(rand(0,999));
//We don’t need a 32 character long string so we trim it down to 5
$security_code = substr($md5_hash, 15, 5);

//Set the session to store the security code
$_SESSION["security_code"] = $security_code;

//Set the image width and height
$width = 80;
$height = 50;

//Create the image resource
$image = imagecreate($width, $height);

//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);

//Make the background black

$fontfile = “./captcha.ttf”;

imagettftext ($image , 30, 0, 10, 36, $black ,$fontfile ,$security_code);

wave_area($image, $x, $y, $width, $height, rand(5,7), $period = 10);

//Tell the browser what kind of file is come in
header(“Content-Type: image/jpeg”);

//Output the newly created image in jpeg format
imagejpeg($image);

//Free up resources
ImageDestroy($image);
}

function wave_area(&$img, $x, $y, $width, $height, $amplitude = 10, $period = 10){

// Make a copy of the image twice the size

$height2 = $height * 2;

$width2 = $width * 2;

$img2 = imagecreatetruecolor($width2, $height2);

imagecopyresampled($img2, $img, 0, 0, $x, $y, $width2, $height2, $width, $height);

if($period == 0) $period = 1;

// Wave it

for($i = 0; $i < ($width2); $i += 2)

imagecopy($img2, $img2, $x + $i – 2, $y + sin($i / $period) * $amplitude, $x + $i, $y, 2, $height2);

// Resample it down again

imagecopyresampled($img, $img2, $x, $y, 0, 0, $width, $height, $width2, $height2);

imagedestroy($img2);

}

create_image();
?>