Location: PHPKode > projects > phpCAMALEO > common/fun2inc/BldImage.php
<?php
/***************************************************************************
 *	BldImage.php
 *
 *	begin		: June 2004
 *	version		: 9 October 2006
 *	copyright	: (C) 2004,2006 grandolini.net
 *
 * 	Generate image for security code in forms
 *
 *		To see the image example:
 *			PHP =>	echo ('<img src="'.$image_name.'">');
 *
 ***************************************************************************/
include_once('Session.php');
include($_SESSION[common].'fun2inc/random_string.inc');
$random_string	=random_string(6);									# random string is 6 chars
$image_name		='tmp/'.date('YmdHis').'.'.$_SESSION[id].'.png';	# check image file
#
if(!function_exists('imagecreate'))
{
	#	GD library is NOT installed
	#
	copy('img/nogdrnd.png',$image_name);

}
else
{
	#	create an image showing the random string
	#
	$width=180; $height=70;	# image size
	#
	include($_SESSION[common].'fun2inc/cleanup_tmpdir.inc');
	$debug	 =false;										# enable to see the image
	$fontpath=$_SERVER[DOCUMENT_ROOT].$_SESSION[misc][folder].$_SESSION[misc][bldimgfont];	# font path/name
	$src	 =$_SERVER[DOCUMENT_ROOT].$_SESSION[misc][folder].$_SESSION[misc][bldimg];		# source image
	#
	cleanup_tmpdir();
	#
	#	draw the image
	#
	if(file_exists($src))
	{
		$imgsrc=@imagecreatefromjpeg($src);
		$ix=@imagesx($imgsrc); $iy=@imagesy($imgsrc);
		$imgdest=@imagecreatetruecolor($width,$height);	$dx=rand(0,$ix-$width); $dy=rand(0,$iy-$height);
		@imagecopy($imgdest,$imgsrc,0,0,$dx,$dy,$ix,$iy);
		$im=$imgdest;
	}
	else
	{
		$im=imagecreate($width,$height);
		$background_color=@imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
		$ii=rand(0,2);	# select kind the of filling
		$objects=rand(12,24);
		for($i=1;$i<=$objects;$i++)
		{
			$randomcolor=@imagecolorallocate($im,rand(128,200),rand(128,200),rand(128,200));
			while($randomcolor==$background_color) { $randomcolor=@imagecolorallocate($im,rand(128,200),rand(128,200),rand(128,200)); }
			switch($ii)
			{
				case 0:	@imagefilledellipse($im,rand(0,$width-10),rand(0,$height-3),rand(20,$height*2),rand(20,$height*2),$randomcolor);	break;
				case 1: @imagefilledrectangle($im,rand(0,$width-10),rand(0,$height-3),rand(20,$height*2),rand(20,$height*2),$randomcolor);	break;
				case 2: unset($tmp); for($t=0;$t<=24;$t++) { $tmp[$t]=rand(0,200); } @imagefilledpolygon($im,$tmp,rand(6,12),$randomcolor); break;
			}
		}
	}
	$border_color=@imagecolorallocate($im,rand(100,255),rand(100,255),rand(100,255));
	@imagerectangle($im,0,0,$width-1,$height-1,$border_color);
	#
	#	text variables
	$fontsize=22;
	$angle=rand(-20,20);
	#
	#	bounding box
	$bbox=imagettfbbox($fontsize,0,$fontpath,$random_string);
	#
	#	baseline point for drawing non-rotated text
	$x0=$bbox[6]; $y0=-$bbox[7];
	#
	#	fixes bounding box w.r.t. image coordinate
	$bbox[5]=-$bbox[5]+$bbox[1]; $bbox[7]=-$bbox[7]+$bbox[3];
	$bbox[1]=0; $bbox[3]=0;
	#
	#	get the size of image
	$sx=imagesx($im); $sy=imagesy($im);
	#
	#	center of bounding box (xc,yc)
	$xc=($bbox[0]+$bbox[2])/2.0; $yc=($bbox[1]+$bbox[7])/2.0;
	#
	#	rotation angle in radian
	$rad=$angle*pi()/180.0;
	$sa=sin($rad); $ca=cos($rad);
	$x1=$x0-$xc; $y1=$y0-$yc;
	#
	#	pivot point (here,we take the center of image)
	$px=$sx/2.0; $py=$sy/2.0;
	#
	#	new baseline point for rotated text
	$x2=intval($x1*$ca+$y1*$sa+$px+0.5); $y2=intval(-$x1*$sa+$y1*$ca+$py+0.5);
	$x3=rand(5,$x2);
	$color=rand(0,2);
	switch($color)
	{
		case 0:	$color='yellow';break;
		case 1:	$color='red';	break;
		case 2:	$color='green';	break;
	}
	switch($color)
	{
		case 'red':		$r_mix=240; $r_max=255; $g_mix=0;   $g_max=70;  $b_mix=0; $b_max=70; break;
		case 'green':	$r_mix=0;   $r_max=70;  $g_mix=240; $g_max=255; $b_mix=0; $b_max=70; break;
		case 'yellow':	$r_mix=240; $r_max=255; $g_mix=240; $g_max=255; $b_mix=0; $b_max=100;break;
	}
	$text_color=@imagecolorallocate($im,rand(30,50),rand(30,50),rand(30,50));
	@imagettftext($im,$fontsize+0.15,$angle,$x3+rand(-3,3),$y2+rand(-3,3),$text_color,$fontpath,$random_string);
	$text_color=@imagecolorallocate($im,rand($r_mix,$r_max),rand($g_mix,$g_max),rand($b_mix,$b_max));
	@imagettftext($im,$fontsize,$angle,$x3,$y2,$text_color,$fontpath,$random_string);
	#
	ob_start();
	if($debug) { header ('Content-type: image/png'); }
	$image=imagepng($im);
	if($debug) { die; }
	#
	$image_data=ob_get_contents();
	ob_end_clean();
	@imagedestroy($im);
	#
	#	write image to disk
	#
	$fd=@fopen($image_name,'w') or die('Cannot write file: '.$image_name);
	$fout=@fwrite($fd,$image_data);
	@fclose($fd);
}
?>
Return current item: phpCAMALEO