Location: PHPKode > projects > Enigma > Enigma2/Enigma2_Install/modules/ModuleCaptcha/image_class.php
<?php
/*
*****************************************************************

 image_class.php
 
*****************************************************************
LSP: Lunabyte Systems Portal
Open-Source Project Inspired by Zef Hemel (hide@address.com)
*****************************************************************
Software Version:                  LSP 2.0 "Enigma 2"
Software by:                         Lunabyte Systems (http://www.lunabyte.net)
Copyright 2002-2005 by:       Lunabyte Systems (http://www.lunabyte.net)
Support, News, Updates at:    http://www.lunabyte.net
*****************************************************************
This program is free software; you may redistribute it and/or modify it 
under the terms of the provided license as published by Lunabyte Systems.

This program is distributed in the hope that it is and will be useful,                  
but WITHOUT ANY WARRANTIES; without even any implied warranty of           
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                        

See the "LSP_license.txt" file for details of the LSP license.
The latest version can always be found at http://www.lunabyte.net.
*****************************************************************

This file is derived from the Jax Captcha Class v1.o1 - Copyright (c) 2005, Andreas John aka Jack (tR)
This file is Open Source in terms of General Public License (GPL) v2.0

*/

class captcha
	{
		var $session_key = null;
		var $temp_dir    = null;
		var $width       = 180;
		var $height      = 70;
		var $jpg_quality = 60;
		
		// Initialize the class

		function captcha($session_key, $temp_dir)
		{
			$this->session_key = $session_key;
			$this->temp_dir    = $temp_dir;
		}
		
				
		 // Generate image

		function _generate_image( $location, $char_seq )
		{
			$num_chars = strlen($char_seq);
			
			$img = imagecreatetruecolor( $this->width, $this->height );
			imagealphablending($img, 1);
			imagecolortransparent( $img );
			
			// generate background of randomly built ellipses
			for ($i=1; $i<=200; $i++)
			{
				$r = round( rand(0, 40 ));
				$g = round( rand(0, 40 ));
				$b = round( rand(30, 150 ));
				$color = imagecolorallocate($img, $r, $g, $b);
				imagefilledarc($img,round(rand(0,$this->width)), round(rand(0,$this->height)), round(rand(0,$this->width/16)), round(rand(0,$this->height/4)), 85, 355, $color, IMG_ARC_PIE);
			}

			$start_x = round($this->width / $num_chars);
			$max_font_size = $start_x;
			$start_x = round(0.5*$start_x);
			$max_x_ofs = round($max_font_size*0.8);
			
			// set each letter with random angle, size and color
			for ($i=0;$i<=$num_chars;$i++)
			{
				$r = round( rand( 127, 255 ) );
				$g = round( rand( 127, 255 ) );
				$b = round( rand( 127, 255 ) );
				$y_pos = ($this->height/2)+round( rand( 5, 20 ) );
				
				$fontsize = round( rand( 18, $max_font_size) );
				$color = imagecolorallocate( $img, $r, $g, $b);
				$presign = round( rand( 0, 1 ) );
				$angle = round( rand( 0, 25 ) );
				if ($presign==true) $angle = -1*$angle;
				
				ImageTTFText( $img, $fontsize, $angle, $start_x+$i*$max_x_ofs, $y_pos, $color, 'modules/ModuleCaptcha/font/default.ttf', substr($char_seq,$i,1) );
			}
			
			// create image file
			imagejpeg($img, $location, $this->jpg_quality);
			imagedestroy( $img );
				
			return true;
		}
		
		
		 // Return name of the image file

		function get_pic($num_chars=8)
		{
			// define characters of which the captcha can consist
			$alphabet = array( 
				'A','B','C','D','E','F','G','H',
				'1','2','3','4','5','6','7','8','9',
				'#', '$', '%', '*' );
				
			$max = sizeof($alphabet);
			
			// generate random string
			$captcha_str = '';
			for ($i=1;$i<=$num_chars;$i++) // from 1..$num_chars
			{
				// choose randomly a character from alphabet and append it to string
				$chosen = rand(1, $max);
				$captcha_str .= $alphabet[$chosen-1];
			}
			
			// generate a picture file that displays the random string
			if ($this->_generate_image( $this->temp_dir.'/'.'cap_'.md5(strtolower($captcha_str)).'.jpg' , $captcha_str))
			{
				$fh = fopen($this->temp_dir.'/'.'cap_'.$this->session_key.'.txt', "w");
				fputs( $fh, md5( strtolower( $captcha_str ) ) );
				return( md5( strtolower( $captcha_str ) ) );
			}
			else 
			{
				return false;
			}
		}
		

		 // check hash of password against hash of searched characters
		function verify( $char_seq )
		{
			$fh = fopen( $this->temp_dir.'/'.'cap_'.$this->session_key.'.txt', "r" );
			$hash = fgets( $fh );
			
			if (md5(strtolower($char_seq)) == $hash)
				return true;
			else 
				return false;			
		}		
	}

?>
Return current item: Enigma