[code=php]
<?php 
/*
	THIS CLASS WILL CREATE A CAPTCHA, MAKE SURE YOU STARTED A SESSION.
	AUTHOR: ARJAN VELDKAMP [ARIE2ZERO.NL]
	VERSION: 0.7
	RELEASED ON 29 APRIL 2009
	
	Use secureform like this way to create a new image:
	$captcha = new secureForm;
	$captcha->secureForm($captcha->code(4), 'mycaptcha', 200, 40, 8);
	
	it will make a new image and a session with a random code with the width of 200px, height of 40px and
	between 4 and 8 lines with the name mycaptcha.png
	
	make sure that the map wich contains this class has rights to write (CHMOD 777)
*/	
class secureForm {

	protected $code;
	protected $ucode;
	protected $width;
	protected $height;
	protected $image;
	protected $lines;
	protected $naam;
	
	/**
	 *	Constructor, will define all information this class needs
	 * 	input string, string, int, int, int
	**/
	public function secureForm($code='Code', $naam='captcha', $width=100, $height=30, $mlines=8) {
		$this->code = str_split($code);
		$this->ucode = $code;
		$this->width = $width;
		$this->height = $height;
		$this->lines = $mlines;
		$this->naam	= $naam;
		$this->image = $this->image();
	}
	
	/**
	 * This function will create the image based on imagesize and characters,
	 *	it wil automaticly choose his position, pick a color and place dots and lines
	 **/	
	private function image() {
		$imgH = $this->height - 20;
		$img = imagecreatetruecolor($this->width, $this->height);
		
		$wit = imagecolorallocate($img, 255, 255, 255);
		imagefill($img, 0, 0, $wit); // the background color
		// add some dots.. because they are funny to..
		$dots = 200;
		for($i=0; $i<$dots; $i++) {
			$red 	= rand(0, 200);
			$green	= rand(0, 200);
			$blue	= rand(0, 200);
			$x = rand(0, $this->width);
			$y = rand(0, $this->height);
			$kleur = imagecolorallocate($img, $red, $green, $blue);
			imagesetpixel($img, $x, $y, $kleur);
		}
		
		$pos = array();
		// make the image variable
		for($i=0; $i < count($this->code); $i++) {
			// pick some colors
			$red 	= rand(0, 200);
			$green	= rand(0, 200);
			$blue	= rand(0, 200);
			if($i == 0) { $edge = rand(0, -20); } else { $edge = rand(-30, 50); }
			$fColor = imagecolorallocate($img, $red, $green, $blue);
			// define some areas to put the letters on
			$pos[$i] = array('width' =>	$this->fontspace() *$i + 1, // position x
							  'height' => rand(20, $imgH), // position y
							  'edge' => $edge); // the edge
			$imgWN = $this->fontspace() * $i;
			imagettftext($img, 14, $pos[$i]['edge'], $pos[$i]['width'], $pos[$i]['height'], $fColor, $this->font(), $this->code[$i]);
		}
		// and now add some lines, just for the fun =D
		$lines = rand(2, $this->lines);
		for($i=0; $i<$lines; $i++) {
			$red 	= rand(0, 200);
			$green	= rand(0, 200);
			$blue	= rand(0, 200);
			$x1 = rand(4, $this->width -30);
			$y1 = rand(4, $this->height - 20);
			$x2 = rand($x1, $this->width);
			$y2 = rand($y1, $this->height);
			$kleur = imagecolorallocate($img, $red, $green, $blue);
			imageline($img, $x1, $y1, $x2, $y2, $kleur);
		}
		imagepng ($img, $this->naam.'.png');
		
		imagedestroy($img);
	}
	/**
	 *	This wil calculate the spacing between the letters
	**/
	private function fontspace() {
		$space = round($this->width / count($this->code), 0);
		return $space;
	}
	
	/**
	 * Define all your fonts here, you have to put them in the same folder as mkimg.php
	 **/
	private function font() {
		$font = array(); // Define all your possible fonts here
		$font[] = 'Market_Deco.ttf';
		$font[] = 'Mouse_Deco.ttf';
		$font[] = 'Fiesta.ttf';
		$aFont = count($font) - 1;
		$tFont = rand(0, $aFont);
		
		return $font[$tFont];
	}
	
	/**
	 * This wil make a random code
	 * input int / output string
	**/
	public function code($maxint) {
		$tekens = array_merge(range('A', 'Z'), range(1, 9));
		$teken = array();
		for($i=0; $i < $maxint; $i++) {
			$wTeken = rand(0, count($tekens) -1);
			$teken[] = $tekens[$wTeken]; 
		}
		$code = implode('', $teken);
		$_SESSION['code'] = $code;
		return $code;
	}
	
	/**
	 * Check of the posted code is even to the image
	 *	input string / output true or false
	**/
		
	 
	public function checkcode($gepost) {
		if(strtoupper($gepost) == $_SESSION['code']) {
			return true;	
		} else { return false; }
	}
	
}

?>