Hi Everyone,
I am very new to php but learning. I have a script that displays random images from a directory. The directory stores the original size image along with the thumbnail image created from the component. The thumbnail image is identified by the prefix ‘thmb_’.
Now my question/problem how do I get the script to select only the thumbnails? I think it has something to do with the preg_match but not sure?
Here is a copy of my script:
function image_rand($dir)
{
if( empty($dir) ) {
return false;
} else if( !is_dir($dir) ) {
return false;
} else if( substr($dir, -1) != ‘/’) {
$dir .= ‘/’;
}
$images = array();
$rh = opendir($dir);
while( $filename = readdir($rh) ) {
if( is_file($dir . $filename) && strstr($filename, ‘.’) ) {
$extension = strrchr($filename, ‘.’);
if( preg_match(‘/^.(bmp|jpe?g|gif|png)$/i’, $extension) ) {
$images[] = $dir . $filename;
}
}
}
if( count($images) > 0 ) {
return $images[ array_rand($images) ];
} else {
return false;
}
}