Location: PHPKode > projects > MJGUEST > mjguest/captcha.jpg.php
<?php
	
	/*
	+---------------------------------------------------------------
	|
	|	MJGUEST
	|	============================================================
	|	Copyright (c) 2002-2008 Giacomo "mdsjack" Menni
	|	Terms of agreement and support at www.mdsjack.bo.it
	|
	+---------------------------------------------------------------
	|	[ INTERFACE MODULE ]
	|	CAPTCHA Security Image Code
	+---------------------------------------------------------------
	*/
	
	// Originally taken from:
	
	/***************************************************************/
	/* PhpCaptcha                                                  */
	/* Copyright © 2005 Edward Eliot - http://www.ejeliot.com/     */
	/* This class is Freeware, however please retain this          */
	/* copyright notice when using                                 */
	/* Last Updated: 26th November 2005                            */
	/***************************************************************/
	
	if (!function_exists('imagecreate')) exit;
	
	define('mjguest_basedir', false);
	
	require ('mjguest.core.php');
	
	$mjguest = new guestbook(false);
	$mjguest->loadmodule('user');
	
	class SecurityImage
	{
		var $oImage;
		var $iWidth;
		var $iHeight;
		var $iNumChars;
		var $iSpacing;
		var $sCode;
		var $font;
		var $font_file;
		var $colors = array();
		
		function SecurityImage($iWidth = 150, $iHeight = 20, $iNumChars = 8)
		{
			$this->iWidth = $iWidth;
			$this->iHeight = $iHeight;
			$this->iNumChars = $iNumChars;
			
			$this->oImage = imagecreate($this->iWidth, $this->iHeight);
			
			$this->iSpacing = (int)($this->iWidth / $this->iNumChars);
		}
		
		function LoadFont()
		{
			$this->font = imageloadfont(realpath($this->font_file));
		}
		
		function DrawBgFuzz()
		{
			for($i = 0, $rd = rand(50, 120), $gr = rand(50, 120), $bl= rand(50, 120), $this->colors[0] = imagecolorallocate($this->oImage, $rd, $gr, $bl); $i <= $this->iHeight; $i+=1){
				$this->colors[1] = @imagecolorallocate($this->oImage, $rd+=5, $gr+=5, $bl+=5);
				@imageline($this->oImage, 0, $i, $this->iWidth, $i, $this->colors[1]);
			}
			
			$bg = imagecolorsforindex($this->oImage, $this->colors[1]);
			$this->colors[2] = imagecolorallocate($this->oImage, $bg['red']+9, $bg['green']+9, $bg['blue']+9);
			unset($bg);
			
			for($i = 0, $x = 0, $z = $this->iWidth; $i < $this->iWidth; $i+=1, $z -= 2, $x += 2){
				@imageline($this->oImage, $x, 0, $x+10, $this->iHeight, $this->colors[1]);
				@imageline($this->oImage, $z, 0, $z-10, $this->iHeight, $this->colors[1]);
			}
		}
		
		function DrawPixels()
		{
			for($i = 0; $i < $this->iWidth; $i++)
			{
				$c = rand(0, 200);
				for($v = 0; $v < 4; $v++)
					imagesetpixel($this->oImage, rand(0, $this->iWidth), rand(0, $this->iWidth), imagecolorallocate($this->oImage, $c, $c, $c) );
			}
		}
		
		function GenerateCode()
		{
			$this->sCode = (string) rand(10000, 99999);
		}
		
		function DrawCharacters()
		{
			$this->colors[3] = imagecolorallocate($this->oImage, 0, 0, 0);
			
		#	imagestring($this->oImage, $this->font, -2, -12, $this->sCode, $this->colors[3]);
			imagestring($this->oImage, $this->font, -1, -11, $this->sCode, $this->colors[2]);
			imagestring($this->oImage, $this->font, 0, -10, $this->sCode, $this->colors[0]);
		}
		
		function Create()
		{
			$this->DrawBgFuzz();
			$this->GenerateCode();
			$this->LoadFont();
			$this->DrawCharacters();
		#	$this->DrawPixels();
			
			if (mjguest_debugmode and !empty($_GET['debug']))
			{
				header('Content-Type: text/plain');
				echo $this->oImage,"\n",$this->GetCode();
				return;
			}
			
			ob_start();
			
			imagejpeg($this->oImage, '', 25);
			
			header('Accept-Ranges: bytes');
			header('Content-Length: '.ob_get_length());
			header('Content-Type: image/jpeg');
			header('Content-Disposition: inline; filename="captcha.jpg";');
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
			header('Cache-Control: no-store, no-cache, must-revalidate');
			header('Cache-Control: post-check=0, pre-check=0', false);
			header('Pragma: no-cache');
			
			ob_end_flush();
			
			imagedestroy($this->oImage);
			
			return true;
		}
		
		function GetCode()
		{
			return $this->sCode;
		}
	}
	
	$captcha = &new SecurityImage(100, 18, 5);
	$captcha->font_file = $mjguest->sys_files['captchafont'];
	$captcha->Create();
	
	$mjguest->user->set('captcha', md5($captcha->GetCode()));
	
	unset($mjguest);
?>
Return current item: MJGUEST