PHP image gallery

To make our image gallery we will make a class that will represent our gallery to make the code usage easier.

function ImageGallery($images = 'images/', $thumbs = 'thumbs/',
                      $thumb_prefix = 'thumb_', $image_types = array('jpg','png','gif'))
{
   $this->images = $images;
   $this->thumbs = $thumbs;
   $this->thumb_prefix = $thumb_prefix;
   $this->image_types = $image_types;
}

Within our class there are four fields. The first will hold the location of the images for the gallery, the second will be the location of the thumbnail images that will be created, the third will be the prefix to add to the thumbnails and finally a list of image types that we can create thumbnails from.

$type = strtolower(end(explode('.', $file)));
if(($file != '.') && ($file != '..') && !is_dir($file) && in_array($type, $this->image_types))
   $all[] = $file;

We will now make a function that lists all the files in a particular directory. This snippet is the most important in this function as it splits the filename into the name and extension and checks that the extension is in the list that we can process. An array is returned of all the files in the given directory that we can make thumbnails from.

$images_all = $this->list_images($this->images);
$thumbs_all = $this->list_images($this->thumbs);
foreach($thumbs_all as $thumb)
{
   $thumb = str_replace($this->thumb_prefix, '', $thumb);
   $key = array_search($thumb, $images_all);
   if($key)
      unset($images_all[$key]);
}
return $images_all;

The next function gets all the thumbnails and compares it to all the images. If the number of thumbnails matches the number of images then we will not make any new thumbnails, otherwise we will return the intersection of the two sets of images so that the appropriate thumbnails can be made.

function create_thumb($filename, $type, $width, $height)
{
   ob_start();
   $create = $this->images.$filename;
   switch ($type)
   {
      case 'jpg':  $image = imagecreatefromjpeg($create); break;
      case 'png':  $image = imagecreatefrompng($create);  break;
      case 'gif':  $image = imagecreatefromgif($create);  break;
      default: die('Cannot create thumbnail.');
}

We will now make the function that makes the thumbnails. We need four inputs into this function: the name of the file to manipulate, the type of the image, and the height and the width of the thumbnail to create. The switch statement makes sure that we can make a thumbnail of that particular type. Output buffering has also been turned on as we do not want the raw image to be sent to the user, we want to send any output to an image file.

$thumb = imagecreatetruecolor($width, $height);
list($image_width, $image_height) = getimagesize($this->images.$filename);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
imagedestroy($image);

Now that we have an image type that we can change the size of, we sample the image to the parameters given in the function and destroy any resources associated with the thumbnail as we have got the output in the output buffer.

switch ($type)
{
   case 'jpg': imagejpeg($thumb); break;
   case 'png': imagepng($thumb);  break;
   case 'gif': imagegif($thumb); break;
}

These functions create the image that we have made.

$new_image = ob_get_contents();
$open = fopen($this->thumbs.$this->thumb_prefix.$filename, 'w');
fwrite($open, $new_image);
fclose($open);
ob_end_clean();

Finally we write the contents of the buffer to a file and give it the name of the original file prefixed with the variable that was set before.

function displayGallery($width, $height)
{
   $thumbs_needed = $this->thumb_checker();
   foreach($thumbs_needed as $new)
   {
      $type = strtolower(end(explode('.', $new)));
      $this->create_thumb($new, $type, $height, $width);
   }
   $all_images = $this->list_images($this->thumbs);
   foreach($all_images as $all)
      echo '<a href="'.$this->images.str_replace($this->thumb_prefix,'',$all).'">
            <img src="'.$this->thumbs.$all.'" border="0" /></a>';
}

To display the images we need to get the files from the thumbnail directory and display them with links to the originals. The parameters to the function give the size of the thumbnails to display.

$gallery = new ImageGallery();
$gallery->displayGallery(40, 40);

To initialise the class you can either give values for the fields or leave them to the default values. When the displayGallery is called we need to specify the size of the thumbnails to display.

Categories

Tags

No tags

Social