Random image from set


I'm trying to build a custom block that will load a random image from a fileset.

Any ideas?

What would be a simple way to get an array of images from a set?


View Replies: View Best Answer
duxferrarie replied on at Permalink Reply
admin replied on at Permalink Reply
Thanks for that.

Not really interested in commercial addons though - Great practice to try and put it together myself.

I've made some progress, but still struggling.
ScottC replied on at Permalink Best Answer Reply
$fs = FileSet::getByID($fileSetID);
 $fileList = new FileList();
 $files = $fileList->get(100,0); //limit it to 100 pictures

cool so files is an array of fIDs, so now you get the count

$size = sizeof($files);
$random = rand(0,$size - 1); //array starts at zero, otherwise you never will get your first image in your set or maybe your last?
$thefile = $files[$random];
//now you can look around on how to display it

The first part is tested, the random stuff looks right but if you have a problem you can debug it and post the finished result here so everyone can learn from it.
admin replied on at Permalink Reply
Many thanks...

I actually did manage to get it working with a very cumbersome route (forgive my atrocious php):

$db = loader::db();
$r = $db->query("SELECT * FROM filesetfiles WHERE fsID = $fileset");
$setimages = array();
while ($row = $r->fetchRow()) {
$setimages[] = ($row['fsfID']);
$count = count($setimages);
$rand = rand(0,$count-1);
$fID = $setimages[$rand];
$f = File::getByID($fID);
$image ='<img src="' . $f->getRelativePath() . '"/><div class="img_title">' . $f->getTitle() . '</div>';
echo $image;

$fileset is the value entered in the block's form.

Does work, but I will try the method above.
admin replied on at Permalink Reply
The output works fine.

But - my block has a text field where you enter the fileset ID - which is not ideal - can someone please advise how to get the list of filesets for my block edit/add view.

Thanks in advance

If this works out nicely I will add the block to the marketplace for free...someone might find a use for it.
ScottC replied on at Permalink Reply
in this case I would just use the following:


$fileSetsAssoc = array();
$myArray = FileSet::getMySets();

foreach($myArray as $ma){
$fileSetsAssoc[$ma->getFileSetID()] = $ma->getFileSetName();
$form = Loader::helper('form');
echo $form->select('fileset-selected',$fileSetsAssoc,$controller->fileSetsAssoc);

$args['fileset-selected'] or $this->post('fileset-selected'); in your controller save method should find the int value there.

olay replied on at Permalink Reply
Hey Dyrk did you have any luck finishing this block? Thanks
waterfeller replied on at Permalink Reply
I would add the following requirement if someone wants to put together a block:

1. The block should allow the site builder to specify a file set. A random image will be chosen from that set, filtered by tag, and displayed with or without a hyperlink.

2. The image will be scaled to best fit the block if necessary. This is needed if the qualifying images are not all the same size.

3. Our requirement would be to filter the images by season, for example, only a SUMMER tagged image would display between the summer solstice and the fall equinox. This could be implemented so it would be automatic, or such that the Admin would have to set an environment variable (less desirable). If generalized, it would allow sitewide changes in images displayed based on season, arbitrary dates, or by setting a global variable.

If someone is interested in building such a block, let us know; we might be able to provide some $-type help.
frz replied on at Permalink Reply
the randomizer add-on should do this if you just put images in sets for seasons and code your block area div to stretch the image as desired..

but yeah, not the getting the season from the date thing.