Rotating photographs

Our webgallery is built on jAlbum, a free album builder. Every night, or whenever manually initiated, it will read all the files from a share on this server and from that (re)build the gallery that is located on http://www.jarnaker.com/jgalleries. This is done with this command (on one line):

D:\\Progra~1\\Java\\j2re1.4.2_11\\bin\\java.exe -Xmx400M -jar  JAlbum.jar
-projectFile \\\juanita\\webgalleries\\2006\\WebGallery.jap  -appendImages

This creates a structure with folder (2006), subfolder (20060818 Misc), and two image folders. The first one is called thumbs and only consists of thumbnails, the second one is called slides and consists of the display sized images as well as a corresponding .html file.First i had a simple script that listed all files that were in a subdirectory called thumbs and then took one random picture from this every time when a page was loaded. It was amazingly fast, 5-6 seconds to cycle through 2700 files in about a 100 directories, on my 800 Mhz machine but not fast enough.

So now I’ve rewritten it to use an index. If the index file date is not the same as todays date then it will rebuild it. Then it will load all thumb paths and names and pick a random one. This file is located in the root of the site and is called by this line in the sidebar php file.

<?php include('gallery_rotate.php'); ?>

The come the script itself called gallery_rotate.php

<?
function getFiles($directory) {
   // Try to open the directory
   if($dir = opendir($directory)) {
       // Create an array for all files found
       $tmp = Array();

       // Add the files
       while($file = readdir($dir)) {
           // Make sure the file exists
           if($file != "." && $file != ".." && $file[0] != '.') {

               // If it's a directiry, list all files within it
               if(is_dir($directory . "/" . $file)) {
			$tmp2 = getFiles($directory . "/" . $file);
                   	if(is_array($tmp2)) {
                       		$tmp = array_merge($tmp, $tmp2);
                   	}
               } else {
			if (strpos($directory, 'thumbs') > 0) {
                   	   array_push($tmp, $directory . "/" . $file);
			}
               }
           }
       }

       // Finish off the function
       closedir($dir);
       return $tmp;
   }
}

function writeIndex() {
	// If the index is not made today, then rebuild it.
	$fileIndex = "getFilesIndex.txt";
	if (date('y d m',filemtime($fileIndex))!= date('y d m')){
		// Delete the old index and build the new one
		$tmp = getFiles('jgalleries');  //This string is where
			// you define what subdirectory the pics are in
		$cnt = 0;
		$fh = fopen($fileIndex, 'w') or die("can't open file");
			while ($cnt < count($tmp)) {
				fwrite($fh, $tmp[$cnt] . "\n");
				$cnt = $cnt + 1;
			}
		fclose($fh);
	}
}

function loadIndex() {
	$fileIndex = "getFilesIndex.txt";
	$fh = fopen($fileIndex, 'r');
	$cnt = 0;
	$theData = fgets($fh);
	while ($theData != "") {
		$tmp[$cnt] = $theData;
		$cnt = $cnt + 1;
		$theData = fgets($fh);		

		if ($cnt == 10000) {
			die("Aborting !! - 10000 lines processed...");
		}
	}
	fclose($fh);
	return $tmp;
}

writeIndex();

$tmp=loadIndex();

mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, count($tmp)-1);
$tmburl=$tmp[$rand];
//print_r($tmburl);
$imgurl=str_replace("/thumbs/","/slides/",$tmburl);
$imgurl=str_replace(".JPG",".html",$imgurl);
//print_r($imgurl);
// ex tmburl = jgalleries/2003/20030619%20Wedding/thumbs/IMG_1835.JPG
// ex imgurl = jgalleries/2003/20030619%20Wedding/slides/IMG_1835.html
?>
<a href="/<?php print_r($imgurl); ?>">
<img src="/<?php print_r($tmburl);?>"></a>

This piece of code is based on Matt Mullenweg’s random image script which resides here

Leave a Comment