« back to folderblog
folderblogwiki

RandomImage

This script calls a random image from a defined folder ignoring folders and thumbs.db file. Uses <?php include(); ?> to display the image. Questions and comments should be directed to Marcus (marcus -at- artitumis -dot- com).

<?php 
/*
# Random Image Generator
# This script will scan a directory of your choosing, ignoring folders and
# the dreaded Thumbs.db file and then return a randum image from the specified
# folder. Easy to customise and use :-) I recommend using include() with this.
#
# This code is free to use and written by Steven Davies and modified by Marcus. If you do use, please
# leave the comment in a code so other web authors can find this site!
#
# This code was taken from www.phub.co.uk
#
# I don't know if I am just retarded or what, but this didn't work exactly how I
# wanted. I edited a couple of things and it worked perfectly.
#
# $dir is your system path to where the script file sits (this is not your url)
# $path is the url path to where the images sit
#
# Use <?php include('path/randomimg.php'); ?> replaceing path with the url path to where the
# images sit to call your image.
*/

/* Customise this line to change to your images folder */
$dir = 'http://www.palebird.com/SALWEBSITE/random/';
$path = 'http://www.palebird.com/SALWEBSITE/random/';
$dh = opendir($dir);
/* The following loop scans the directory specified ignoring folders and Thumbs.db */
while (false !== ($filename = readdir($dh)))
{
if($filename <> "Thumbs.db" || !(is_dir($filename)))
{
$tempArray = explode(".", $filename);
/* Add image file types in the code below */
if($tempArray[1] == "jpg" || $tempArray[1] == "gif" || $tempArray[1] == "png" || $tempArray[1] == "tif" || $tempArray[1] == "bmp")
{
$files[] = $filename;
}
}
}

/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);

/* print the result */
echo "<IMG SRC='$path/$files[$randnum]' ALT='$files[$randnum]' BORDER='0'>
";
echo "<!--Code Written By Steven Davies : www.phub.co.uk and customized by Marcus of artitumis.com -->";

?>
Home  Index  Recent Changes  folderblog
Last edited February 23, 2008 at 10:51pm.(edit this entry)