Directory-based random image rotation PHP script
23rd May 2007
Yesterday I needed to put together a rather simple PHP script: it would read the contents of a single pre-configured directory, and randomly select up to a pre-configured number of files. These files were images, and were just dumped as IMG tags into the webpage. I came up with a solution, shown below.
The script is simple, but still it’s easier to use the ready solution than to write your own :).
It is heavily commented, and should be easy to understand.
To copy-paste, first click the “PLAIN TEXT” box header.
- // configuration
- // total number of images we want to display; can be 1 as well
- $to_display = 10;
- // type of files to use eg. .jpg or .gif or .png
- $file_types = array(".jpg", ".gif", ".png");
- // the location of the folder containing the images
- $image_folder = "/usr/local/vhosts/sitename/htdocs/images";
- // URL prefix for all images' SRC attribute;
- // can be just "/", or full URL
- $URL_prefix = "http://some-site.kiev.ua/images/";
- // no configuration below this comment!
- $Images = array();
- // check if $image_folder is really a directory
- if (is_dir($image_folder))
- {
- $handle = opendir($image_folder);
- // read *all* files from the $image_folder
- while (false !== ($img = readdir($handle)))
- {
- // skip two "special files",
- // current and upper directories
- if ($img != "." && $img != "..")
- {
- // extract file's extension and test if it
- // is present in the $file_types array;
- // if yes - add file to the Images array
- $typetest = strtolower(substr($img,-4));
- if ( in_array($typetest, $file_types) )
- $Images[] = $img;
- }
- }
- closedir($handle);
- }
- // if there are less images than $to_display,
- // decrease the number to actual
- if ( count($Images) < $to_display)
- $to_display = count($Images);
- // randomly select $to_display images
- $rand_keys = array_rand($Images, $to_display);
- // output IMG tags with random images
- foreach($rand_keys as $rand_key)
- echo '<img src="'.$URL_prefix.$Images[$rand_key].'" alt="" />';
How to use: insert this code into your PHP file where you need to output the list of images. Do not forget to apply some pretty CSS styling to IMG tags!
The major drawback of this solution is inefficiency: reading the complete directory of image files on every page display is not good at all. However, I inserted this code snippet into the CMS’s PHP-enabled template, which allows to turn on caching. This actually solves efficiency problem, as directory will be read (and images randomized…) only as often as long your configured cache is.
Another option is to add in some caching options. It might be reasonable as well to use MySQL HEAP-table (in-memory table) to store the directory-reading result and refresh it only once in an hour; this way images would be randomized on each page display, but the directory would be re-read less than once in an hour.
But all of the above-described enhancements are optional, and can be left as an exercise to the reader