<?php
session_start();

//////////////////////////////////////////
// Abzetdin Z. Adamov
// Copyrights © 2008
// All rights reserved.
//
// Last modified 11/20/2010
// This program is open-source PHP Captcha, which helps to make sure 
// that only humans perform certain actions with your website, 
// protecting it from spam bots (software).
// Requires: PHP and GD module
//////////////////////////////////////////

header("Content-type:image/gif");

$captcha_length = 4;		// the number of symbols in captcha
$font_name = 'alba.ttf';	// the font
$font_size = 26;			// the size 
$captcha_angle = rand(-8, 8);; // the angle of captcha string

// Depending on what kind of symbols (Numbers, Chars, Chars and Numbers) you would like to see on your captcha, leave oppropriate lines and remove others...

//--Random Numbers
$numbs = range ('0', '9');
shuffle($numbs);
$captcha_str = implode(array_slice($numbs, 0, $captcha_length));

//--Random Chars
$chars = range ('A', 'Z');
shuffle($chars);
$captcha_str = implode(array_slice($chars, 0, $captcha_length));

//--Random Chars and Numbers
$chars = range ('A', 'Z');
$numbs = range ('0', '9');
$symbs = array_merge($chars, $numbs);
shuffle($symbs);
$captcha_str = implode(array_slice($symbs, 0, $captcha_length));

// Define Session with the value of captcha string 
$_SESSION['captch'] = "$captcha_str";

// Create captcha image
$backgroundimage = "images/security_background.gif";
$im=imagecreatefromgif($backgroundimage);
$color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));

$bbox = imagettfbbox ($font_size, $captcha_angle, $font_name, $captcha_str); 
$captcha_width = $bbox[2] - $bbox[0];
$x -= $captcha_width / 2;

imagettftext($im, $font_size, $captcha_angle, 50+$x, 30, $color, $font_name, $captcha_str);
imagegif($im);
imagedestroy($im);
     
?>