Display random image?

Permalink
I am trying to grab a random image from a file set to display as a background. I am close, but can't quite get it to work... here's what I have so far:

Loader::model('file_list');
Loader::model('file_set');
$fs = FileSet::getByName('background_home');
$fileList = new FileList();
$fileList->filterBySet($fs);
$fileList->filterByType(FileType::T_IMAGE);
$files = $fileList->get(100,0); //limit it to 100 pictures

$size = sizeof($files);
$random = rand(0, $size - 1);
$theFile = $files[$random];

Then I have this - which right now just displays a static image from my images folder. I want to replace this with the image I retrieved above...

img src="<?php echo $this->getThemePath()?>/images/bg_home.jpg" class="bgimage"/>

Any help much appreciated.

 
12345j replied on at Permalink Reply
12345j
replace src="whatever" with
src="<?php echo $theFile->getPath();?>"
dmennenoh replied on at Permalink Reply
Thanks! It doesn't actually work, but that definitely helped.

What I get is this:


img src="c:\inetpub\wwwroot/files/4613/2519/1783/bg_salonsspas2.jpg" class="bgimage"/>


Not sure if it's because there's a mix of forward and back slashes, or it's the c: and not an actual URL...
12345j replied on at Permalink Best Answer Reply
12345j
ahh, right. try
src="<?php echo $theFile->getRecentVersion()->getRelativePath();?>"
dmennenoh replied on at Permalink Reply
Perfect! Thanks a ton!