Posted on: 04/25/02 01:51pm
By: Anonymous (Anonymous)
Tony,
I was just reading the article on how to add PHP Block for displaying random Gallery photos. In a comment that you wrote, you referred to the following page:
http://www.geeklog.net/samplecode/gallery_geeklog_random_pic.txt
That page is no longer there so I was wondering where it may have gone. The thread that I was reading is:
http://www.geeklog.net/article.php?story=20020205061244619
Fixed
Posted on: 04/25/02 02:19pm
By: Tony
This is fixed...left of shit from our site upgrade.
Fixed
Posted on: 04/25/02 03:12pm
By: Anonymous (AlphaDominion)
I'm glad somone posted this! Thanks!
I just tried integrating the code and I found one typo that will cause a missing function error. Instead of making people figure it out on their own I figured that I would just repost the code here. I've also added the fix to avoid selecting an empty album.
Put the following code in your lib-common.php file: (Sorry for the lack of formatting)
/**
* This is the function that returns a random image from the Gallery
*
* NOTE: this does not like albums with no pictures in it!
*
*/
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';
// Bail if we have a bad path
if (!is_dir($path_gallery_albums)) return "invalid directory";
// Open album directory
$fd = opendir($path_gallery_albums);
$albums = array();
$index = 1;
// Load all albums into an arrray. NOTE: this should be modified at
// some point to ignore empty albums!!!!!!
while (($f = @readdir($fd)) == TRUE) {
if (is_dir($path_gallery_albums . $f) && $f <> '.' && $f <> '..' && $f <> 'CVS' && ($f <> '.users')) {
clearstatcache();
$albums[$index] = $f;
$index++;
}
}
closedir($fd);
if (count($albums) == 0) return $retval .= "no albums found";
// Randomly select an album from the array
mt_srand((double)microtime()*1000000);
$indexr = (mt_rand(1, count($albums)));
$rand_album = $albums[$indexr];
// Now get a random picture from this album
$thepic = phpblock_random_files($path_gallery_albums . $rand_album);
$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=' . $rand_album . '&id=' . $pic_prefix;
$retval .= "\">";
$retval .= "<img src=\"";
$retval .= $gallery_album_url . '/' . $rand_album . '/' . $thepic;
$retval .= "\">";
$retval .= "</a><br></div>\n";
return $retval;
}
/**
* Given an album this will return a random Gallery thumbnail from that album
*
* @g string Path to gallery album to get a random image from
*
**/
function phpblock_random_files($g)
{
// Open directory
$fd = opendir($g);
$pics = array();
$index = 1;
// Loop through directory inserting all thumbnails into an array. Do not add
// .gif images because netpbm can't convert them.
while (($f = @readdir($fd)) == TRUE) {
if ($f <> '.' && $f <> '..' && $f <> 'CVS' && strpos($f,'thumb') && !strpos(strtolower($f),'.gif')) {
clearstatcache();
$pics[$index] = $f;
$index++;
}
}
closedir($fd);
if ($index > 2) {
mt_srand((double)microtime()*1000000);
$indexr = (mt_rand(1, count($pics)));
} else {
$indexr = 1;
}
// Return image name
return $pics[$indexr];
}
Oh yeah, one more thing
Posted on: 04/25/02 03:19pm
By: Anonymous (AlphaDominion)
I wasn't able to figure out how to prevent images in hidden albums from being displayed.
If anyone has the solution, then can you please post it.
Thanks!
Filtering out other albums
Posted on: 04/25/02 05:11pm
By: efarmboy
You can add more album names to the if statement that is filtering out files when loading albums into the array.
Example - Filtering out hobbies and my music files:
while (($f = @readdir($fd)) == TRUE) {
if (is_dir($path_gallery_albums . $f) && $f <> 'Hobbies' && $f <> 'music' && $f <> '.' && $f <> '..' && $f <> 'CVS' && ($f <> '.users')) {
clearstatcache();
$albums[$index] = $f;
$index++;
Cheers,
Blaine