Date created: Sunday, March 13, 2011 9:40:50 PM. Last modified: Friday, September 15, 2017 10:38:40 AM

Image Rotor

After seeing a random image/banner rotator written in PHP whilst looking at a Drupal site, I decided to write my own. This simply needs to be point ad a directory that contains the images that require rotation (upon page refresh). Its similar to the Drupal rotator I saw except the Drupal rotator printed out a random image as a raw picture rathen than in HTML.

Also, the main reason I wrote this alternate is becuase I needed a rotator that supported image maps. By placing a text document in the images directory with the same name as an image, it will be loaded and used as an image map;

<?php
/*
 * Banner/image rotator with link maps, 
 * James Bensley, 02-2011
 *
*/

// Is rotating on (1) or shall we show the default banner (0)
$rotating = 1;

// Full path to the folder where the images are [with trailing slash]
$imageFolder = '/home/r00tb00t/www/r00tb00t.seal-brown.feralhosting.com/public_html/img/';

// HTTP path to be returned in to the page [with trailing slash]
$httpPath = '/img/';

// Default image used when banner rotation is turned off (inside the imageFolder path)
$defaultImage = 'selfphoto.jpg';

// Array of allowed file extensions and mime type mappings [lower case only]
$fileTypes = array();
$fileTypes[0] = 'gif';
$fileTypes[1] = 'jpeg';
$fileTypes[2] = 'jpg';
$fileTypes[3] = 'png';

//$imageInfo = null;

if(!is_dir($imageFolder)) // Make sure a valid image folder is supplied
{
 echo $imageFolder.' is not a directory dummy!';
 die();
}

if ($rotating==0) // If we aren't rotating show our default banner
{
	$imageInfo = pathinfo($imageFolder.$defaultImage);
	if (file_exists($imageFolder.$imageInfo['basename'])) // Make sure the default file exists
	{
		displayImg(pathinfo($imageFolder.$defaultImage), $imageFolder, $httpPath); // Pass the image on to be displayed
	}else{
		echo 'Default image doesn\'t exist you buffoon!'.$httpPath.$defaultImage;
		die();
	}
}else // Else, we are rotating so get a list of images in the banners folder
{
	$fileList = array();
	$handle = opendir($imageFolder);
	while (false !== ($file = readdir($handle)))
	{
		for($i=0;$i<count($fileTypes);$i++) { // Check this image is a permited file type
			$imageInfo = pathinfo($file);
			if ($fileTypes[$i] == strtolower($imageInfo['extension'])) {
				$fileList[] = $file; // build up a list
			}
		}
	}
	closedir($handle);

	if (count($fileList) > 0) { // Once we have built up a list, that actually has stuff in it!..
		$imageNumber = rand(0, (count($fileList)-1)); // Randomly pick one and parse it on to the display function
		displayImg(pathinfo($fileList[$imageNumber]), $imageFolder, $httpPath);
		//Admittedly the random part above is a bit weak but I don't care, I'm not counting the bloody occurrences!
	}else{
		echo 'There are no images in the image folder dummy!';
		die();
	}
}

// This function will display the image parsed but also check to see if a text file exists 
// of the same name, which can contain an image map, if so it will be used

function displayImg($img, $baseFolder, $baseURL)
{

	$mapFile = $baseFolder.$img['filename'].'.txt';
	if(file_exists($mapFile)) // Check for a link map
	{
		echo '<img src="'.$baseURL.$img['basename'].'" usemap="#bannermap">';
		include($mapFile);
	}else{
		echo '<img src="'.$baseURL.$img['basename'].'">';
	}

}
?>
Example image map:
<map name="bannermap">
<area shape="rect" coords="38,18,147,112" href="/" alt="Home" />
<area shape="rect" coords="206,39,270,99" href="anotherpage" alt="Another Page" />
<area shape="rect" coords="270,62,375,83" href="andathird.html" alt="The Third Page" />
</map>