Jaslabs: High performance Software

High Performance Software

How to write an image gallery script in PHP

Introduction 

This is a simple yet useful tutorial on how to write an image gallery script in PHP.  It could be used for anything from banner rotation to just a couple of images that you would like to display in rotation on your website.  It requires no external libraries, works with php 4.0.3 and above, and will allow you to display all the images in a specifiied directory.

The Code

The code consists of a simple class with two main functions: GetAllFiles() and DisplayImages(). GetAllFiles() retrieves all the images within a specified directory and DisplayImages randomly displays them.

//set your directory here and remember to include the trailing slash
//the script will automatically pull images out of the specified directory and display them
define(”IMAGE_DIRECTORY”, “images/”);

class jrotate {

var $Imagelist = array();         //this stores all of the images that were found in your directory
var $ImageExtList = array();      //this will store the image extensions

function GetAllFiles() {

// Open a known directory, and proceed to read its contents
if (is_dir(IMAGE_DIRECTORY)) {
   if ($dh = opendir(IMAGE_DIRECTORY)) {
       while (($file = readdir($dh)) !== false) {
        //check to see if it is a file
        if (filetype(IMAGE_DIRECTORY.$file) == “file”) {
              //get the files extension and make sure it is one of our supported image types
              $current_ext = substr($file,strlen($file)-3,strlen($file));
              if ($current_ext == “jpg” || $current_ext == “gif” || $current_ext == “png”) {
                 //push the filename  and extension onto our arrays
                 array_push($this->Imagelist,IMAGE_DIRECTORY.$file);
                 array_push($this->ImageExtList,$current_ext);
              }
          }
       }
       closedir($dh);
   }
 } 
}

function DisplayImage() {
 
 //get the total number of images
 $imagecount = count($this->Imagelist);
 //randomly generate the next image that we will display
 $nextimage = rand(0,$imagecount-1);
 
 //determine the image type and return the proper header information
 switch($this->ImageExtList) {
 case ‘png’:
   header(”Content-type: image/png”);
 break;
 case ‘gif’:
   header(”Content-type: image/gif”);
 break;
 case ‘jpg’:
      header(”Content-type: image/jpeg”);
 break;
}

 //read image into string and output it
 $contents = file_get_contents($this->Imagelist[$nextimage]);
 echo $contents;
}

} //end class

//create a new instance of the jrotate class
$imagerotator = new jrotate;

//display random image
$imagerotator->GetAllFiles();
$imagerotator->DisplayImage();
//$test = array();

 Download

To download the entire script explained here and an example of usage, please go Here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

2 Comments so far

  1. hobot December 14th, 2006 12:12 pm

    Thanks man, I was looking for a simple way to do this, you made my day.

Leave a reply