PHP Function Display Random Image
This is the function to diplay random image from folder. You can use this function to display banner on your website as well as you can show galley image randomly just like other popular news websites. With little javascript tweak, you can also create slide show. It will cover this in my upcoming post.
Required Directory Permission
Image Folder: 555
Image Itself: 555
<?
function get_random_image($image_dir)
{
Initialize variable
$images=”;//get directory object
$img_obj = dir($image_dir);//scan folder for allowed file extension
while ($f = $img_obj->read()) {
if (eregi(“gif”, $f) || eregi(“jpg”, $f) || eregi(“png”, $f))
$images .= “$f “;}
closedir($img_obj->handle);//creating image array
$img_array = explode(” “, $images);
$num = sizeof($img_array)-2;//generate a random number between 0 and the number of images
$random = mt_rand(0, $num);
$img = $img_array[$random];//return image code
return “<img src=’”.$image_dir.$img.”‘ border=’0′>”;
}
?>
Read Related Post
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.












































Leave a Reply