Location: PHPKode > projects > PBL Guestbook > pbl-guestbook/imgcode.php
<?php
// Image Code Library v 1.0
// written by Lev <hide@address.com>
// free scripts @ www.pixelatedbylev.com

/* DEDICATION / INSPIRATION ----->
To the lazy man, to make things easier.
*/

///////////////////////////////////
// NOTHING NEEDS TO BE MODIFIED! :)
///////////////////////////////////

/*
NO DATABASE REQUIRED!!! BASED ON MATH, ENVIRONMENT VARIABLES, AND TIME!
GUARANTEED TO GIVE THE SAME UNIQUE CODE TO EACH CLIENT!

to use this library simply use the following line in your script(s):

   require ("imgcode.php");

example call for image (text color,background color,font size,font file,background image):

   image_code("000000","000000","30","C:/Program/Apache Group/Apache/htdocs/pbl/fonts/freak.ttf","pics/imgcodebg.gif");

example call for verification:

   if ($_POST['code'] == get_image_code())
      {
      // success...
      }
   else
      {
      // failure...
      }
*/


function image_code ($text_color,$bg_color,$font_size,$font_face,$bg_image)
	{
	$code = get_image_code();
	$bg_color = str_replace("#","",$bg_color);
	$text_color = str_replace("#","",$text_color);
	$gdarray = gd_info();
	if ($gdarray["FreeType Support"])
		{
		$codedem = imagettfbbox ($font_size, 0, $font_face, $code);
		$width = $codedem[2] - $codedem[0] + 6;
		$height = $codedem[1] - $codedem[7] + 6;
		$xpos = 3;
		$ypos = $height - 6;
		header("Content-type: image/png");
		$im = imagecreatetruecolor($width, $height);
		if ($bg_image != '')
			{
			list($bgwidth, $bgheight) = getimagesize($bg_image);
			if (eregi("\.gif",$bg_image))
				{
				$image = imagecreatefromgif($bg_image);
				}
			elseif (eregi("\.png",$bg_image))
				{
				$image = imagecreatefrompng($bg_image);
				}
			else
				{
				$image = imagecreatefromjpeg($bg_image);
				}
			imagecopyresampled($im, $image, 0, 0, 0, 0, $width, $height, $bgwidth, $bgheight);
			}
		else
			{
			sscanf($bg_color, "%2x%2x%2x", $red, $green, $blue);
			$background = imagecolorallocate($im, $red, $green, $blue);
			imagefill($im, 0, 0, $background);
			}
		sscanf($text_color, "%2x%2x%2x", $red, $green, $blue);
		$text = imagecolorallocate($im, $red, $green, $blue);
		imagettftext($im, $font_size, 0, $xpos, $ypos, $text, $font_face, $code);
		imagepng($im);
		imagedestroy($im);
		}
	else
		{
		header("Content-type: image/png");
		$im = imagecreatetruecolor(60,20);
		if ($bg_image != '')
			{
			list($bgwidth, $bgheight) = getimagesize($bg_image);
			if (eregi("\.gif",$bg_image))
				{
				$image = imagecreatefromgif($bg_image);
				}
			elseif (eregi("\.png",$bg_image))
				{
				$image = imagecreatefrompng($bg_image);
				}
			else
				{
				$image = imagecreatefromjpeg($bg_image);
				}
			imagecopyresampled($im, $image, 0, 0, 0, 0, 60, 20, $bgwidth, $bgheight);
			}
		else
			{
			sscanf($bg_color, "%2x%2x%2x", $red, $green, $blue);
			$background = imagecolorallocate($im, $red, $green, $blue);
			imagefill($im, 0, 0, $background);
			}
		sscanf($text_color, "%2x%2x%2x", $red, $green, $blue);
		$text = imagecolorallocate($im, $red, $green, $blue);
		imagestring ($im, 5, 4, 1, $code, $text);
		imagepng($im);
		imagedestroy($im);
		}
	exit;
	}


function get_image_code ()
	{
	$code = strtoupper(substr(md5(date("d",time())),0,3));
	$s2 = round(strlen($_SERVER['SERVER_NAME']) / 2);
	$fc = substr($code,0,1);
	if (strlen($s2) == 1)
		{
		$s2 = $s2;
		}
	else
		{
		for ($x = 0; $x < strlen($s2); $x++)
			{
			$count = 0;
			for ($i = a; $i < j; $i++)
				{
				if ($count == $s2{$x})
					{
					if ($s2c == $i)
						{
						$i++;
						$s2c = $i;
						}
					else
						{
						$s2c = $i;
						}
					}
				$count++;
				}
			$ns2 .= strtoupper($s2c);
			}
		$s2 = $ns2;
		}
	if (preg_match("/^[0-9]$/",$fc))
		{
		if ($s2 > ($fc / 2))
			{
			$code .= $s2;
			}
		else
			{
			$code = $s2 . $code;
			}
		}
	else
		{
		$code = $s2 . $code;		
		}
	list($ip1,$ip2,$ip3,$ip4) = split("\.",$_SERVER['REMOTE_ADDR']);
	if (preg_match("/^[A-Z]$/",substr($code,0,1)))
		{
		if ($ip2 > $ip4)
			{
			$code = $ip4 . $code;
			}
		else
			{
			$code = $ip2 . $code;
			}
		}
	else
		{
		if ($ip2 > $ip4)
			{
			$code = $code . $ip4;
			}
		else
			{
			$code = $code . $ip2;
			}
		}
	if (strlen($code) > 6)
		{
		$code = substr($code,1,6);
		}
	return $code;
	}


?>
Return current item: PBL Guestbook