index.php (place in each directory)

<?php include  $DOCUMENT_ROOT "/Photography/gallery.php"?>

gallery.php

<?php 
    
include($DOCUMENT_ROOT "/Templates/templates.php"); 

    
// Photo gallery script
    // Copyright 2005, Oliver White
    // GPL license
    //
    // Note: do not use non-standard filenames or directory names without checking this
    // script for html-injection vulnerabilities.  (e.g. no "tes&lt;script.jpg")
    //
    // Prior to using, create an empty directory with world-writeable permissions, for thumbnails
    //
    // If any thumbnails get out-of-date, either set CheckThumbnail()'s $CreateAll variable to 1,
    // or delete the relevant thumbnails if you know which ones they are (hint: look in web browser)
    //
    // Search for "SETUP" comments in this code to see things which should be checked when installing
    // the software in a new location
    //

    // Location from which the script is being run
    
$Location $_SERVER["REQUEST_URI"];
    
    
// Directory to search
    
$Dir ".";

    
$Name ParseURL($Location);
    
CheckThumbnailDir($Dir);
    
$FileList ListFiles($Dir);
    
natsort($FileList);

    
// SETUP: page header
    
document_header($Name);
    
document_navbar(); 

    print 
"<p><a href=\"../\">Up one level</a></p>\n";
    
ListImages($FileList$Dir);
    
ListDirectories($FileList$Dir);
   
    function 
ParseURL($URL){
      
// Return a pretty-formatted version of the URL
      
$Parts explode("/"$URL);
      return 
$Parts[max(count($Parts)-20)];
    }
    function 
ListImages($FileList$Dir){
      
// Lists a set of images in HTML form
      
print "<p>\n";
      foreach(
$FileList as $File){
        
$FullFile "$Dir/$File";
        if(
is_file($FullFile)){
          if(
is_image($File)){
            
$Thumbnail thumbnail_name($File);
            
CheckThumbnail($File$Thumbnail);
            
$ThumbURL htmlentities(thumbnail_url($File));
            
$URL htmlentities(urlencode($File));
            
$Name htmlentities($File);
            
// SETUP: Format of photo gallery images
            
printf("<a href=\"%s\"><img class=\"preview\" src=\"%s\" alt=\"%s\"></a>"$URL$ThumbURL$Name);
          }
        }
      }
      print 
"</p>\n";
    }
    function 
ListDirectories($FileList$Dir){
      
// Lists a set of subdirectories in HTML form
      
foreach($FileList as $File){
        
$FullFile "$Dir/$File";
        if(
is_dir($FullFile)){
        
          
// Ignore directory names starting with a full-stop
          
if(ShowDirectory($File$FullFile)){
            
$URL htmlentities(urlencode($File)) . "/";
            
$Text htmlentities($File);
                        
            
// Look for an icon.txt file in the directory, which suggests thumbnail images
            
$DirectoryIcon GetDirectoryIcon($FullFile);
            if(
$DirectoryIcon){
              
$DirectoryIconImage thumbnail_name($DirectoryIcon);
              
CheckThumbnail($DirectoryIcon$DirectoryIconImage);
              
$ThumbURL htmlentities(thumbnail_url($DirectoryIcon));

              
// SETUP: Format of directory listings, with thumbnail
              
$Text "<a href=\"$URL\"><img class=\"gallery_preview\" src=\"$ThumbURL\" alt=\"$File\"></a> <a href=\"$URL\">$File</a>";
            }
            else
            {
              
// SETUP: Format of directory listings, without thumbnail
              
$Text "<a href=\"$URL\">$File</a>";
            }
            print 
"<div class=\"gallery\">$Text</div>";
          }
        }
      }
    }
    function 
ShowDirectory($Dir$FullDir){
      if(
substr($Dir,0,1) == ".")
        return(
0);
      if(
is_file($FullDir "/nolink.txt"))
        return(
0);
      return(
1);
    }
    function 
CountItems($Dir){
      
// Not yet in use: counts various things in a directory
      
$List ListFiles($Dir);
      
$CountDirs 0;
      
$CountFiles 0;
      foreach(
$List as $Item){
        if(
is_dir($Item))
          
$CountDirs++;
        if(
is_file($Item))
          
$CountFiles++;
      }
      return(array(
$CountDirs$CountFiles$CountDirs $CountFiles));
    }
    function 
GetDirectoryIcon($Dir){
      
// For a given directory, look for the file named in its icon.txt
      
$ReturnVal "";
      
$Filename1 $Dir "/icon.txt";
      if(
file_exists($Filename1)){
        if(
$fp fopen($Filename1,"r")){
          
$Line fgets($fp,100);
          
$Filename2 $Dir "/" rtrim($Line);
          if(
file_exists($Filename2)){
            
$ReturnVal $Filename2;
          }
          
fclose($fp);
        }
      }
      return(
$ReturnVal);
    }
    function 
CheckThumbnail($File$Thumbnail){
      
// Check that the given thumbnail exists, and recreate it if necessary
      // Set this to 1 to regenerate all thumbnails
      
$CreateAll 0;
      if(
is_file($Thumbnail) && ! $CreateAll)
        return;
      
CreateThumbnail($File$Thumbnail);
    }
    function 
CreateThumbnail($File$Thumbnail){
      
// Create a thumbnail version of the given image
      // SETUP: Thumbnail size and quality
      
$Quality 75;   // JPEG compression, percent
      
$Size 150;    // Thumbnail image size, pixels

      // Load image based on its file-type
      
if(preg_match("/\.jpe?g$/i",$File))
        
$Image imagecreatefromjpeg($File);
      else if(
preg_match("/\.gif$/i",$File))
        
$Image imagecreatefromgif($File);
      else if(
preg_match("/\.png$/i",$File))
        
$Image imagecreatefrompng($File);
      else
        return;
      if(!
$Image)
        return;
      
      
// Calculate the height and width of the thumbnail (and of the original image)
      
$Width1 imagesx($Image);
      
$Height1 imagesy($Image);
      if(
$Width1 || $Height1 1)
        return;
        
      if(
$Width1 $Height1){
        
$Width2 $Size;
        
$Height2 $Height1 $Width2 $Width1;
      }
      else
      {
        
$Height2 $Size;
        
$Width2 $Width1 $Height2 $Height1;
      }
      
      
// Create a new blank image for the thumbnail
      
$Image2 imagecreatetruecolor($Width2$Height2);
      if(!
$Image2)
        return;
        
      
// Copy the data from the main image into the thumbnail
      
imagecopyresampled($Image2$Image0,0,0,0,$Width2+1,$Height2+1,$Width1,$Height1);
      
      
// Save the thumbnail (convention here is that all thumbnails are JPEG)
      
imagejpeg($Image2$Thumbnail$Quality);
    }
    function 
CheckThumbnailDir(){
      
// TODO: create the thumbnail directory if it doesn't already exist 
      // (does the typical apache even have permission to do this?)
      // maybe just display a warning if it doesn't exist
    
}
    function 
thumbnail_name($File){
      
// For a given image, return its thumbnail's filename
      
global $DOCUMENT_ROOT;
      
$Partial thumbnail_url($File);
      return( 
$DOCUMENT_ROOT $Partial);
    }
    function 
thumbnail_url($File){
      
// For a given image, return its thumbnail's relative URL
      // SETUP: Set this to your thumbnails directory
      
$Hash MD5($File);
      return(
"/Photography/.thumbnails/$Hash.jpg");
    }
    function 
is_image($File){
      
// Test whether a file should be treated as an image (i.e. thumbnail+link)
      
$Pattern '/^\w+\.(jpg|jpeg|png|gif)$/i';
      return(
preg_match($Pattern$File));
    }
    function 
ListFiles($Dir){
      
// Return a list of all files and subdirectories in a directory
      
$FileList = Array();
      if(
$fp opendir("./")){
        while(
$File readdir($fp)){
          if(
$File != ".." && $File != "."){
            
array_push($FileList$File);
          }
        }
        
closedir($fp);
      }
      return(
$FileList);
    }    
  

  
// BlibbleBlobble.co.uk only - remove this line if you've downloaded the script
  
document_footer();
?>