<?
	include("../encrypt/encrypt.php");

	$code = $_GET[ "code" ];

	$enc = new Encrypt;
	$codeDec = $enc->dec($code);
	unset($enc);

	$code = $codeDec;

	
	/* Our TTF font file, you may use others */
	$font = dirname(__FILE__) .'/fonts/arial.ttf';
	
	/* set the width */
	$width = 16;
	//$width  = (strlen($code) * $width) + 2;	
	$height = 24;
	
	$code_length = 4;
	
	$image_height = $height + 2;
	$image_width = $width * $code_length + 20;
	
	$im = imagecreatetruecolor($image_width, $image_height);
	$white = imagecolorallocate($im, 255, 255, 255);
	imagefill($im, 0, 0, $white);
	
	
	/* Some themes */
	$theme = array();

	$theme[] = array(
									'CHAR_COLOR' => 
										array('R' => array(150, 201),
													'G' => array(250, 252),
													'B' => array(0, 126) 
										),
									'BG_COLOR' =>
										array('R' => array(150, 230),
													'G' => array(150, 230),
													'B' => array(150, 230) 
										)
						 );
	$theme[]	= array(
									'CHAR_COLOR' => 
										array('R' => array(23, 172),
													'G' => array(235, 255),
													'B' => array(1, 163) 
										),
									'BG_COLOR' =>
										array('R' => array(230, 255),
													'G' => array(170, 230),
													'B' => array(170, 230) 
										)
						 );
	$theme[]	= array(
									'CHAR_COLOR' => 
										array('R' => array(0, 125),
													'G' => array(138, 250),
													'B' => array(58, 178) 
										),
									'BG_COLOR' =>
										array('R' => array(194, 230),
													'G' => array(197, 230),
													'B' => array(230, 255) 
										)
						 );
	
	$pos_x = 5;
	$pos_y = 20;
	$random = rand(0, (count($theme) - 1) );/* Get a random theme */
	

	/**
	 *	Place each character into the image 
	**/
	$angle = 0;
	$size = 16;
	for($i = 0, $count = strlen($code); $i < $count; $i++) {
					
		$color = imagecolorallocate($im, 
														rand($theme[$random]['CHAR_COLOR']['R'][0], $theme[$random]['CHAR_COLOR']['R'][1]), 
														rand($theme[$random]['CHAR_COLOR']['G'][0], $theme[$random]['CHAR_COLOR']['G'][1]), 
														rand($theme[$random]['CHAR_COLOR']['B'][0], $theme[$random]['CHAR_COLOR']['B'][1])
													);
		
		imagettftext($im, $size, $angle, $pos_x, $pos_y, $color, $font, $code{$i});
		$pos_x  += $width + 1;

	}
		
	/* Finally show image */
	imagepng($im);
	imagedestroy($im);

	/* Unset Variables */
	unset($color, $size, $angle, $random, $pos_y, $pos_x, $theme, $white, $im, $image_width, $image_height, $code_length, $height, $width, $font);
?>