Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

  • Exits

  • Categories

  • Archives

  • Visitors’ track

    Locations of visitors to this page
  • Tags list

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.

PHP:
  1. // configuration
  2. // total number of images we want to display; can be 1 as well
  3. $to_display = 10;
  4.  
  5. // type of files to use eg. .jpg or .gif or .png
  6. $file_types = array(".jpg", ".gif", ".png");
  7.  
  8. // the location of the folder containing the images
  9. $image_folder = "/usr/local/vhosts/sitename/htdocs/images";
  10.  
  11. // URL prefix for all images' SRC attribute;
  12. // can be just "/", or full URL
  13. $URL_prefix = "http://some-site.kiev.ua/images/";
  14.  
  15. // no configuration below this comment!
  16.  
  17. $Images = array();
  18.  
  19. // check if $image_folder is really a directory
  20. if (is_dir($image_folder))
  21. {
  22.  $handle = opendir($image_folder);
  23.  
  24.  // read *all* files from the $image_folder
  25.  while (false !== ($img = readdir($handle)))
  26.  {
  27.   // skip two "special files",
  28.   // current and upper directories
  29.   if ($img != "." && $img != "..")
  30.   {
  31.    // extract file's extension and test if it
  32.    // is present in the $file_types array;
  33.    // if yes - add file to the Images array
  34.    $typetest = strtolower(substr($img,-4));
  35.    if ( in_array($typetest, $file_types) )
  36.     $Images[] = $img;
  37.   }
  38.  }
  39.  closedir($handle);
  40. }
  41.  
  42. // if there are less images than $to_display,
  43. // decrease the number to actual
  44. if ( count($Images) <$to_display)
  45.  $to_display = count($Images);
  46.  
  47. // randomly select $to_display images
  48. $rand_keys = array_rand($Images, $to_display);
  49.  
  50. // output IMG tags with random images
  51. foreach($rand_keys as $rand_key)
  52.  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 ;)

  • Share/Bookmark

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>