Random image from set
Permalink
Hey
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?
Dyrk
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?
Dyrk
http://www.concrete5.org/marketplace/addons/randomizer/
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.
Not really interested in commercial addons though - Great practice to try and put it together myself.
I've made some progress, but still struggling.
$fs = FileSet::getByID($fileSetID); $fileList = new FileList(); $fileList->filterBySet($fs); $fileList->filterByType(FileType::T_IMAGE); $files = $fileList->get(100,0); //limit it to 100 pictures
cool so files is an array of fIDs, so now you get the count
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.
Many thanks...
I actually did manage to get it working with a very cumbersome route (forgive my atrocious php):
$fileset is the value entered in the block's form.
Does work, but I will try the method above.
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.
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.
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.
in this case I would just use the following:
Loader::model('file_set');
$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.
-Scott
Loader::model('file_set');
$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.
-Scott
Hey Dyrk did you have any luck finishing this block? Thanks
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.
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.
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.
but yeah, not the getting the season from the date thing.