I was dissatisfied with the random image block not producing really random images (images in smaller albums would come up more frequently). Also the poor handling of empty albums and the desire to have a list of excluded albums prompted me to rework the Random Image Block for Gallery/Geeklog.
I found the original block while surfing, so I'm afraid I don't know who to ascribe the original work to. Also, this seems to run a bit slower (and bigger in memory) than the original as it builds an array with the name of every picture in Gallery.
Without further ado, here it is:
/**
* This is the function that returns a random image from the Gallery
*/
function phpblock_gallery()
{
global $_CONF;
// Location of the Gallery album directory
$path_gallery_albums = $_CONF['path_html'] . 'albums/';
// URL to the gallery
$gallery_url = $_CONF['site_url'] . '/gallery';
// URL to album directory
$gallery_album_url = $_CONF['site_url'] . '/albums';
// Albums we want excluded
$ignore_albums = array('album01', 'album02', 'album03');
// Bail if we have a bad path
if (!is_dir($path_gallery_albums)) return "invalid directory";
// Open album directory
$fd = opendir($path_gallery_albums);
// Load all pictures into an array. We do this but itering over the
// albums available, then selecting all the thumnails from them.
$pics = array();
while (($f = @readdir($fd)) == TRUE) {
if (is_dir($path_gallery_albums . $f) && !ereg('hidden',$f) && $f <> '.' && $f <> '..' && $f <> 'CVS' && ($f <> '.users') && !in_array($f, $ignore_albums)) {
$dir = opendir($path_gallery_albums . $f);
$fcount = 0;
while (($g = @readdir($dir)) == TRUE) {
if ($g <> '.' && $g <> '..' && $g <> 'CVS' && strpos($g,'thumb') && !strpos(strtolower($g),'.gif')) {
clearstatcache();
$pics[] = $f . '/' . $g;
}
}
closedir($dir);
}
}
closedir($fd);
if (count($pics) == 0) return $retval .= "no pictures found";
// Randomly select a picture from the array
mt_srand((double)microtime()*1000000);
$indexr = (mt_rand(1, count($pics)));
$thepic = $pics[$indexr];
// Now get a random picture from this album, album name, and pic prefix
$slashpos = strpos($thepic, '/');
$album = substr($thepic, 0, $slashpos);
$thepic = substr($thepic, $slashpos - strlen($thepic) + 1);
$pos = strpos($thepic, '.');
$pic_prefix = substr($thepic, 0, $pos);
// build HTML output and return it
$retval .= "<div align=center> <a href=\"";
$retval .= $gallery_url . '/view_photo.php?set_albumName=' . $album . '&id=' . $pic_prefix;
$retval .= "\">";
$retval .= "<img border=0 src=\"";
$retval .= $gallery_album_url . '/' . $album . '/' . $thepic;
$retval .= "\">";
$retval .= "</a> <br></div>\n";
$retval .= "<div align=center> <a href=\"";
$retval .= $gallery_url . "\">More Pictures</a>";
$retval .= "<br></div>\n";
return $retval;
}