Making a Quick PHP Gallery with Thumbnail Generation

http://www.64Lines.com »» Making a Quick PHP Gallery with Thumbnail Generation «

Overview

I often see fellow web developers with their own domain names using web services such as Flickr to host a very simple gallery with only a handful of pictures. I prefer to host my own content since it keeps users on my side and allows me to customize the gallery layout as well as display my own advertisements. If you think it is complicated or tiresome to create a simple gallery from a directory of images, then think again - the code below fits in only 64 lines and will create the thumbnails directory automatically and list the files within it. You should specify the password on the first line of the make thumbnails script since it is a memory intensive script which could bring down a server if repeatedly called - to activate it pass the secret password in the URL (make_thumbs.php?pass=mypassword).

Source Code - make_thumbs.php

Function to create thumbnails in 33 lines <?php
if($_GET['pass'] != 'password') die('access denied');
error_reporting(E_ALL);
if(!is_dir('thumbs')) mkdir('thumbs') or die('can\' create thumbs directory');
$file_list = array();

if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
      if (strtolower(array_pop(explode('.',$file))) == 'jpg') {
         $file_list[] = $file;
      }
   }
   closedir($handle);
}

$count = 0;
$total = count($file_list);
foreach($file_list as $file) {
   $save_path = getcwd().'/thumbs/';
   $im = imagecreatefromjpeg($file);
   $new_x = imagesx($im) / 10;
   $new_y = imagesy($im) / 10;
   $small = imagecreatetruecolor($new_x,$new_y);
   imagecopyresampled($small,$im,0,0,0,0,$new_x,$new_y,imagesx($im),imagesy($im));
   imagejpeg($small,$save_path.$file,85);
   imagedestroy($im);
   imagedestroy($small);
   usleep(100);
   set_time_limit(90);
   $count++;
   echo "Working on file {$count} / {$total}<br>\n";
   flush();
}
?>

Source Code - view_thumbs.php

Once the thumbnails have been created you can view them using the view_thumbs.php file. It has extra filtering enabled to hide any pictures that do not have a full sized version (such as deleted pictures), therefore if you need to delete an image you could only delete the full sized original.

Function to view the thumbnails in 43 lines <?php
error_reporting(E_ALL);
if(!is_dir('thumbs')) die('thumbs directory was not found');
$file_list = array();

if ($handle = opendir('thumbs')) {
   while (false !== ($file = readdir($handle))) {
      if (strtolower(array_pop(explode('.',$file))) == 'jpg') {
         $file_list[] = $file;
      }
   }
   closedir($handle);
}

$count = 0;
$total = count($file_list);
?>
<html>
<head>
<title>Gallery</title>
<style type="text/css">
body, html {
   width: 90%;
   text-align: center;
}
img.pic {
   float: right;
   margin: 20px;
   border: 0px;
}
</style>
</head>
<body>
<?php
sort($file_list);
foreach($file_list as $file) {
   $path = 'thumbs/'.$file;
   if(!is_file($file)) continue;
   echo "<a href=\"{$file}\"><img src=\"{$path}\" class=\"pic\" alt=\"{$file}\"></a>
";
}
?>
</body>
</html>

Source Code - index.php

Linking to the view_thumbs.php file is often cumbersome, and if indexes are enabled on the server simply asking for the directory path could return the contents of the directory. I would suggest creating an index file within the main folder which simply includes the view_thumbs.php file.

Function to use index.php as view_thumbs.php in 1 line <?php include('view_thumbs.php'); ?>

Change Log

References

Image resizing using PHP - good code examples of how to resize an image using PHP
This script in action - photo-shoot of my apartment - Uses the scripts above to create the gallery

If you happen to use this script on one of your galleries then please feel free to send me an email and get a link to your site on this page.


 ________________________________________ 
/ 'Cow says: Thank you for visiting. You \
| are browsing: Making a Quick PHP       |
| Gallery with Thumbnail Generation. You |
| have been to 1 pages on this site      |
\ today.'                                /
 ---------------------------------------- 
       \   ,__,
        \  (oo)____
           (__)    )\
              ||--|| *

Check out the other 64Lines.com articles, tutorials, and tools

All article content is copyright of Michael Petrov, 2006©.
The source code presented in the articles is distributed as freeware, please feel free to use it in your own projects - both commercial and non commercial.
Valid HTML 4.01 Transitional Valid CSS!