Random Image UDT
Posted: Wed Mar 04, 2009 7:42 pm
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
Usage:
Next version will have subdirectory recursive image listing, if I receive some feedback, and some of you need it.
Regards!
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)];
Code: Select all
{rnd_image dir="uploads/images/random" exclude="thumb"}
Regards!