Page 1 of 1

Random Images

Posted: Wed Jan 31, 2024 10:19 am
by caigner
The scenario: The user has uploaded multiple images via File Manager or Gallery.

Now I want to display a random selection of these images on a webpage, like this:

One Image:

Code: Select all

{random_image dir="images/Gallery/tiles" assign="myImage" urlonly=1}

<img src="{$myImage}">
Multiple images:

Code: Select all

{random_image dir="images/Gallery/tiles" count=6 assign="myImageCollection" urlonly=1}

{foreach $myImageCollection as $myImage}
   <div><img src="{$myImage}"></div>
{/foreach}

Is there already a solution out there?

If not, how should I go about to solve this task? By creating a User Defined Tag, or a Plugin/Module?

Regards,
Christian

Re: Random Images

Posted: Wed Jan 31, 2024 3:19 pm
by WDJames
Hi Christian,

This is the UDT (named "random_image") I use to display a random image from a folder:

Code: Select all

$images = glob("".$params['folder']."/*.{jpg,jpeg,png,gif}", GLOB_BRACE);
$randomImage = str_replace('thumb_', '', $images[array_rand($images, 1)]);
echo $randomImage;
<img src="{random_image folder='path/to/your/folder'}">

With multiple images, I just use shuffle on the Gallery/Module template.

Code: Select all

{capture}{$images|@shuffle}{/capture}
{foreach from=$images item=image}
...
{/foreach}
I'm sure there are better ways of doing the above but these works for me.

I hope this helps.

Thanks,

James

Re: Random Images

Posted: Wed Jan 31, 2024 6:05 pm
by caigner
Hi James,

Thanks for you help. I'll try it first thing tomorrow morning.

Best regards,
Christian

Re: Random Images

Posted: Sat Feb 03, 2024 2:40 pm
by ILoveTravel1
caigner wrote: Wed Jan 31, 2024 6:05 pm Hi James,

Thanks for you help. I'll try it first thing tomorrow morning.

Best regards,
Christian
Hi Christian,
Iam interested in the result. It would be nice if you give us some feedback :)

Best Rene