<?php
/*
HIVEWARE IMAGE ROTATOR
Version 2.1 - October 22, 2003
Copyright (c) 2002-2003 Dan P. Benjamin, Hiveware Corp.
All Rights Reserved.
[url]http://www.hiveware.com/imagerotator.php[/url]
DISCLAIMER
Hiveware makes no representations or warranties about
the suitability of the software, either express or
implied, including but not limited to the implied
warranties of merchantability, fitness for a particular
purpose, or non-infringement. Dan P. Benjamin and Hiveware
shall not be liable for any damages suffered by licensee
as a result of using, modifying or distributing this
software or its derivatives.
ABOUT
This PHP script will randomly select an image file from a
folder of images on your webserver. You can then link to it
as you would any standard image file and you'll see a random
image each time you reload.
When you want to add or remove images from the rotation-pool,
just add or remove them from the image rotation folder.
VERSION CHANGES
Version 1.0
- Release version
Version 1.5
- Tweaked a few boring bugs
Version 2.0
- Complete rewrite from the ground-up
- Made it clearer where to make modifications
- Made it easier to specify/change the rotation-folder
- Made it easier to specify/change supported image types
- Wrote better instructions and info (you're them reading now)
- Significant speed improvements
- More error checking
- Cleaner code (albeit more PHP-specific)
- Better/faster random number generation and file-type parsing
- Added a feature where the image to display can be specified
- Added a cool feature where, if an error occurs (such as no
images being found in the specified folder) *and* you're
lucky enough to have the GD libraries compiled into PHP on
your webserver, we generate a replacement "error image" on
the fly.
Version 2.1
- Updated a potential security flaw when value-matching
filenames
INSTRUCTIONS
1. Modify the $folder setting in the configuration section below.
2. Add image types if needed (most users can ignore that part).
3. Upload this file (rotate.php) to your webserver. I recommend
uploading it to the same folder as your images.
4. Link to the file as you would any normal image file, like this:
<img src="http://example.com/rotate.php">
5. You can also specify the image to display like this:
<img src="http://example.com/rotate.php?img=gorilla.jpg">
This would specify that an image named "gorilla.jpg" located
in the image-rotation folder should be displayed.
That's it, you're done.
*/
/* ------------------------- CONFIGURATION -----------------------
Set $folder to the full path to the location of your images.
For example: $folder = '/user/me/example.com/images/';
If the rotate.php file will be in the same folder as your
images then you should leave it set to $folder = '.';
*/
$folder = '.';
/*
If you'd like to enable additional image types other than
gif, jpg, and png, add a duplicate line to the section below
for the new image type.
Most users can safely ignore this part.
*/
$extList = array();
$extList[] = 'gif';
$extList[] = 'jpg';
$extList[] = 'png';
$extList[] = 'jpeg';
// You don't need to edit anything after this point.
// --------------------- END CONFIGURATION -----------------------
function valueMatch($haystack, $needle) {
$ext = substr( strrchr($needle, '.'), 1, strlen($needle) );
foreach($haystack as $value) {
if ($ext==$value) {
return true;
}
}
return false;
}
$img = null;
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}
if ( isset($_GET['img']) && valueMatch($extList, $_GET['img']) ) {
$tmp = $folder . $_GET['img'];
if (file_exists($tmp)) {
$img = $tmp;
}
} else {
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
foreach ($extList as $e) {
if ( preg_match('/'.$e.'$/i', $file) ) {
$fileList[] = $file;
}
}
}
closedir($handle);
mt_srand( (double) microtime ()* 1000000 );
$tmp = $folder.$fileList[ mt_rand( 0, sizeOf($fileList)-1 ) ];
if (file_exists($tmp)) {
$img = $tmp;
}
}
if ($img!=null) {
$ext = strrchr($img,'.');
if ($ext==".jpg") $ext = ".jpeg";
$contentType = 'Content-type: image/' . substr( $ext, 1, (strlen($ext)) );
header ($contentType);
readfile($img);
} else {
if (function_exists(imagecreate) ) {
header ("Content-type: image/png");
$im = @imagecreate (100, 100) or die ("Cannot initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 0,0,0);
imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
imagepng ($im);
imagedestroy($im);
}
}
?>