Random Image UDT

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
viebig

Random Image UDT

Post by viebig »

Recently some users reported difficulties using the random_image tag

I wrote a lighter UDT to handle random_images with a exclude param, and image file extension filtering

name it rnd_image

Code: Select all

$dir = (empty($params['dir'])) ? "uploads/images" : $params['dir'];
$myDir = dir($dir);
if(!$myDir) return "<!-- Directory '".$params['dir']."' not exist, aborting -->";
$images = Array();
while($file = $myDir->read()) {
if($file != '.' && $file != '..' && !is_dir($params['dir'].$file)) {	
$ext = strtolower(substr(strrchr($file, "."), 1));	
if(($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") && strstr($file, $params['exclude']) == "") {
$images[] = $file;
}}}
$myDir->close();
if (sizeof($images) == 0) return "<!-- No images found matching image extensions and excluded pattern -->";
return $dir.'/'.$images[rand(0,sizeof($images)-1)];
Usage:

Code: Select all

{rnd_image dir="uploads/images/random" exclude="thumb"}
Next version will have subdirectory recursive image listing, if I receive some feedback, and some of you need it.

Regards!
Pierre M.

Re: Random Image UDT

Post by Pierre M. »

JeremyBASS

Re: Random Image UDT

Post by JeremyBASS »

what would be nifty ...


{rnd_image dir="uploads/images/random" exclude="thumb" ext="jpg,png" }

it'd be handy to use for other items then like css and flv's swfs and all... just a thought as the first glace looked like it returns the src and not the full component, making this possible..


Cheers
jeremyBass
viebig

Re: Random Image UDT

Post by viebig »

Thanks Pierre and Jeremy,

I will improve this UDT adding file extension selective support, axclude and include params, and subdirectory recursive option.

Regards
alby

Re: Random Image UDT

Post by alby »

viebig wrote: I will improve this UDT adding file extension selective support, axclude and include params, and subdirectory recursive option.
For information only:
multiple-random-image plugin has subdirectory, maxdepth, allowextension, includeprefix, excludeprefix and url params
it's a multiple display image (with separator) but for number=1 display 1 image only

Alby
viebig

Re: Random Image UDT

Post by viebig »

Hi Alby,

Thanks for that info. Will save me a lot of job!

That remembers me about the need of a well structured UDT directory for CMSMS folks, the forge and wiki dont help too much for standard users. The module manager is a great feature, maybe we should have udt/tag manager.

Thank you
Post Reply

Return to “Tips and Tricks”