March Madness - Clock Image in PHP

March Madness - Clock Image in PHP

This is a clock generated bia the image library in PHP. The background color was selected to match the Fubarlabs color scheme.
Hit RELOAD to update the clock.

This is a simple example of how to dynamic images within PHP.



<?php

	$imSize			=	512;
	$clockSize		=	$imSize - 40;

	$today			=	getdate();
	$hour			=	$today[hours];
	$minutes		=	$today[minutes];
	$seconds		=	$today[seconds];
	if ($hour > 12)
	{
		$hour	-=	12;
	}
	
	$hourDegress	=	$hour * 30;
	$hourDegress	+=	$minutes / 2;
	$minDegress		=	$minutes * 6;
	
	//*	the cordinate system stars at 3:00
	$hourDegress	-=	90;
	if ($hourDegress < 0)
	{
		$hourDegress	+=	360;
	}
	$minDegress	-=	90;
	if ($minDegress < 0)
	{
		$minDegress	+=	360;
	}
	
	//*	convert to radians
	$hourRadians	=	$hourDegress / 57.295779513082;
	$minRadians		=	$minDegress / 57.295779513082;
	
	$myImage		=	ImageCreate($imSize, $imSize);
	
	$white			=	ImageColorAllocate($myImage, 255, 255, 255);
	$black			=	ImageColorAllocate($myImage, 0, 0, 0);
//	$red			=	ImageColorAllocate($myImage, 255, 0, 0);
	$green			=	ImageColorAllocate($myImage, 50, 151, 0);
//	$blue			=	ImageColorAllocate($myImage, 0, 0, 255);

	ImageFill($myImage, 0, 0, $green);
	Imageinterlace($myImage, 1);

	//*	draw the clock circle
	Imagearc($myImage, $imSize/2, $imSize/2, $clockSize, $clockSize, 0, 359, $white);

	//*	draw the hour hand
	$hourHandLen	=	($clockSize / 2) * 0.6;
	$x2				=	$imSize/2 + ($hourHandLen * cos($hourRadians));
	$y2				=	$imSize/2 + ($hourHandLen * sin($hourRadians));
	ImageLine($myImage, $imSize/2, $imSize/2, $x2, $y2, $white);

	//*	draw the minute hand
	$minHandLen		=	($clockSize / 2) * 0.9;
	$x2				=	$imSize/2 + ($minHandLen * cos($minRadians));
	$y2				=	$imSize/2 + ($minHandLen * sin($minRadians));
	ImageLine($myImage, $imSize/2, $imSize/2, $x2, $y2, $white);


	if ($minutes < 10)
	{
		$minutes	=	"0$minutes";
	}
	Imagestring($myImage, 3, 5, $imSize - 15, ($hour . ":" . $minutes), $white);

	Imagestring($myImage, 3, 5, 15, "March Madness", $white);


	header("Content-type: image/jpg");
	imagejpeg($myImage);

	Imagedestroy($myImage);
 
?>