Page 1 of 1

Random Image UDT

Posted: Wed Mar 04, 2009 7:42 pm
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!

Re: Random Image UDT

Posted: Fri Mar 06, 2009 6:22 pm
by Pierre M.

Re: Random Image UDT

Posted: Fri Mar 06, 2009 7:06 pm
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

Re: Random Image UDT

Posted: Sat Mar 07, 2009 9:53 pm
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

Re: Random Image UDT

Posted: Sat Mar 07, 2009 10:03 pm
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

Re: Random Image UDT

Posted: Sat Mar 07, 2009 11:36 pm
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