<?php
session_start();

$string = '';

for ($i = 0; $i < 5; $i++) {
	$str = rand(0,1);
	if($str == 0)
	{
		$string .= chr(rand(48, 57));
	}
	else
	{
		$string .= chr(rand(97, 122));
	}
}

$_SESSION['random_number'] = $string;
$dir = '../../../themes/client/default/fonts/';
//echo "dir: ".$dir."<br>";
$image = imagecreatetruecolor(165, 50);

// random number 1 or 2
$num = rand(1,2);
if($num==1)
{
	$font = "Capture it 2.ttf"; // font style
}
else
{
	$font = "Molot.otf";// font style
}

// random number 1 or 2
$num2 = rand(1,2);
$rgb = hex2rgb('#'.$_GET['clr']);
if($num2==1)
{
	$color = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);// color
}
else
{
	$color = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);// color
}
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);
//echo "font: ".$dir.$font."<br>";
imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION['random_number']);

header("Content-type: image/png");
imagepng($image);
function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   return $rgb; // returns an array with the rgb values
}
?>