I'm very proud of this little baby because it's the first script I created 100% on my own. It isn't much and I'm sure there are much better ways to do it, but it's my baby.
This script will allow you to display images in a set directory randomly. To see this script in action, click here and reload, reload, reload.
There is at least one caveat that I know about – images must exist in one directory with no sub directories. Theoretically, this shouldn't be a problem, but I've had problems with the script when trying to run it in a directory with subdirectories.
So what will you need?
a) PHP The page you display this script on must have a php extension and your server should be able to read PHP. It's a lifeblood. Get used to it.
b) a directory of images residing on your server Most people organize their site enough that their images are already in a directory by their lonesome. You can also pull only certain extensions or endings, as will be described later. But for example, if you name all your thumbnails: image-thumb.jpg you can pull all your thumbnails up randomly.
And enough of my babble, here's the meat of the script. Pay attention to anything red for later:
<?
# Put the SERVER PATH to image directory.
# INCLUDE trailing slash
$dir = "/home/username/public_html/pathto/yourphotos/";
# Put the HTML PATH to image directory.
# DO NOT include trailing slash
$html_dir = "http://www.yoursite.com/pathto/yourphotos";
$dir_handle = opendir($dir);
$image_array = array();
# This opens up the image directory, grabs all files with a ".jpg" extension
# and puts them into an array. You can set whatever extension you'd like here.
# Just switch out the .jpg for .gif, etc. If you have more than one extension,
# replace this line with something like this:
# if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg") || (substr($file, -4) == ".gif"))) {
# This will pull up all .jpg OR .gifs. If you name all the images you want to pull
# a certain way (example, image-thumb.jpg or image.thumb.jpg) you can do this:
# if (($file !=".") && ($file !="..") && (substr($file, -10) == ".thumb.jpg")) {
# basically you want to put the extension in quotes and then count how many characters
# it is and place that after $file, –
while ($file = readdir($dir_handle)) {
if (($file !=".") && ($file !="..") && (substr($file, -4) == ".jpg")) {
$file_name = "$html_dir/$file";
$image_array[$file_name] = $file_name;
}
}
# sorts the array. For some reason this won't work at all
# unless this is done. Can anyone tell me why?
sort($image_array);
$count = sizeof($image_array);
# trims all the URLs of whitespace and assigns each
# item in the array a number to identify it.
for($i = 0;$i<sizeof($image_array); $i++) {
$image[$j++] = rtrim($image_array[$i]);
}
# assign a random value
$r = rand(0,$count-1);
# get the size of a random image
$size = getimagesize($image[$r]);
$width = $size[0];
$height = $size[1];
# This is the HTML output. You can change any of this, but you should
# leave src=\"$image[$r]\" width=\"$width\" height=\"$height\" where it is.
# Remember to escape any quotation marks, etc.
$img_src = "<img src=\"$image[$r]\" width=\"$width\" height=\"$height\" alt=\"This is a Random Image\" />";
# closes the directory (why keep it open?)
closedir ($dir_handle);
?>
Okay, pretty straight forward. You want to replace the first two red things with the SERVER path to your image directory and the HTML path to your image directory, respectively. Use the example as a guide.
The third colored thing, the BLUE one, you'll only need to pay attention to if you want to add extensions or specify end-of-file-names, such as image-thumb.jpg. So I'll skip that and come back to it in a minute.
You want to place this code near the top of your HTML document. I usually place stuff like this right below the BODY tag to make sure it's above the place you're actually going to call the image.
Then, wherever you want to place the random image, place this code:
<? echo $img_src; ?>
Simple, no? 😉
Okay, and the last little bit. Say you have all your thumbnails named image.thumbnail.jpg The .thumb.jpg never changes so you can tell the script to call up .thumb.jpg and ONLY display THOSE images.
To do that, you'd replace the above BLUE code with the following (exactly, character for character or you'll get a parse error):
(substr($file, -10) == ".thumb.jpg")
Here's how that works. the "-10" above is how many characters from the end the script should count. the text inside the quotes, .thumb.jpg is 10 characters and you're telling the script that's what should appear.
You can do a variety of things with this. If you wanted to pull up all .jpg AND all .gif files, you'd replace the BLUE text above with the following:
((substr($file, -4) == ".jpg") || (substr($file, -4) == ".gif"))
If you wanted to EXCLUDE all thumbnails using the above example, you'd place the following:
(substr($file, -10) != ".thumb.jpg")
It goes on and on.
—
Again, as this is the first (lengthy) script I've done completely on my own, there may be bugs and I may have done something the hard way. I appreciate any comments with quirks, questions or suggestions for a more effecient way to do this.