Showing posts with label random image. Show all posts
Showing posts with label random image. Show all posts

Monday, June 18, 2012

Random Image Display

Many times we get bored without any reason.. if this is one of those moments then don't worry, coding will make the moment a little bit lighter;

We'll create a webpage that'll display a random image on each visit(refresh) to the page. PHP will be the language in which we'll create the webpage.

First of all, the RandomImage class,

class RandomImage {
    private $ImageDir;
    private $FileList;
   
    /* this is list of extension which will be allowed to display as an image */
    private $FilterList = array("jpg", "jpeg", "gif", "png");
   
    /* constructor */
    public function __construct($initDir) {
        $this->ImageDir = $initDir;
        $this->FileList = array();
    }
   
    /* function to filter out files based on extension */
    private function IsFileOkToShow($file){
        $isOk = false;
        /* check if file extension is not in the filter list */
        $matched = false;
        $fileExt = pathinfo($file, PATHINFO_EXTENSION);
        foreach ((array)$this->FilterList as $ext) {
            if(strcasecmp($fileExt, $ext) == 0) {
                $matched = true;
                break;
            }
        }
        if($matched) {
            $isOk = true;
        }
   
        return $isOk;
    }
   
    /* this function will scan through all the directories(in recursive manner)
     * and build the list of files in all directories.
     */
    private function buildImageList($dir) {
        if($handle = opendir($dir)) {
            while(false !== ($entry = readdir($handle))) {
                if($entry == '.' || $entry == '..') {
                    continue;
                }
                $fullPath = $dir.'/'.$entry;
                if(is_dir($fullPath)) {
                    $this->buildImageList($fullPath);
                } else {
                    if($this->IsFileOkToShow($fullPath)) {
                        array_push($this->FileList, $fullPath);
                    }
                }
            }
            closedir($handle);
        }
    }
   
    /* public function to return a random file */
    public function Get() {
        $this->buildImageList($this->ImageDir);
        srand((float)microtime()*1000000);
        $fileCount = count($this->FileList);
        if($fileCount > 0) {
            $randomIndex = rand(0, $fileCount -1);
            return $this->FileList[$randomIndex];
        } else {
            return "foo!";
        }
    }
}

This class is very simple and it has only one public function Get(). Calling the Get() function on an object of this class will scan through the directory and return a random file from it.

Upon receiving the filename, we'll pass this file to 'src' attribute of <img> tag, in order to display it.

<!--  html code to display the image -->
<html>
<head><title>Ignorance is bliss!</title></head>
<body>
<?php
    $image = new RandomImage("PATH_TO_IMAGE_FOLDER");
?>
    <img src="<?php echo $image->Get();?>">
<?php
?>
</body>
</html>

Thats all!
Download the attached source code and start clicking on refresh button!