More than 1 random image
Posted: Tue Dec 07, 2004 10:27 am
I've been trying to get this for a few hours and can't seem to do it. Trying to select say 5 random images form a directory for a Gallery. I can sort them all fine, but rand() doesn't seem to work.
This particular setup gives me 'h' for file names??? Thanks.
Code: Select all
<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder></image_folder>
<time>3</time><fade>2</fade><repeat>true</repeat>
<captions>true</captions></settings><images>';
create_tree("albums/userpics/10010", '/^thumb_/');
function create_tree($file_dir, $filter = '') {
global $xml;
if ($handle = @opendir($file_dir))
{
$list = array();
while (false !== ($file = @readdir($handle))) {
if ($file != "." && $file != "..") {
if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
if( !preg_match($filter, $file) ) // get only "thumbl_" prefixed images
continue;
$list[] = $file;
}
}
$random = array_rand($list, 5);
foreach($list as $file) {
if( is_dir($file_dir . '/' . $file) ) {
create_tree($file_dir . "/" . $file, $filter);
}
else
$xml .= '<image>' . '<file><![CDATA[' . $file_dir . '/' . $file[$random] . ']]></file>' . '<caption>' . '</caption>' . '</image>' . "\n";
}
closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>