<?php
//directory with original pics
$img_dir "./";
//directory for thumbnail files
$thumbs_dir "../thumbs/";
//dimensions of thumbnails
$thumb_width 120;
$thumb_height 80;
//thumbnails in one row
$columns 3;
//thumbnails on one page
$maxperpage 9;
//htmlcode around
$templatefile "../template.txt";
//save generated thumbnails? faster!
$save true;
//regular expression for filemask
// ".+\.png$|.+\.jpg$" = all .png and .jpg
// ".+\.jp[e]{0,1}g$" = all .jpeg and .jpg
// ".+\.gif" = all .gif
$ext_pattern ".+\.jp[e]{0,1}g$";
//caption of thumbnail table
$infocaption "Click on a thumbnail to view the photograph";


// creates a thumbnail image from the original
function thumb_create($img,$width,$height,$thumb_width,$thumb_height
{
  
$thumbnail imagecreatetruecolor($thumb_width,$thumb_height);
  
  
// maintain the aspect ratio of the image
  
$max_height $width * ($thumb_height $thumb_width);
  if (
$height $max_height) {
      
$height $max_height;
  }
  
imagecopyresampled($thumbnail,$img,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
  return 
$thumbnail;
}


// sends the thumbnail back to browser.  if the thumbnail does not already exist,
// it is generated.  if $save is set to 'true', the image is cached
function thumb_print($dir,$file_thumb,$save,$mdir,$thumb_width,$thumb_height
{
  
// if thumbnail does not exist already, create it
  
if (!(file_exists($mdir.$file_thumb))) {
      list(
$width,$height,$pictype) = getimagesize($dir.$file_thumb);
      switch (
$pictype) {
          case 
$img imagecreatefromgif($dir.$file_thumb); break;
          case 
$img imagecreatefromjpeg($dir.$file_thumb); break;
          case 
$img imagecreatefrompng($dir.$file_thumb); break;
      }
      if (
$img) {
          
$thumbnail thumb_create($img,$width,$height,$thumb_width,$thumb_height);
          if (
$save) {
            switch (
$pictype) {
              case 
imagegif($thumbnail,$mdir.$file_thumb); break;
              case 
imagejpeg($thumbnail,$mdir.$file_thumb); break;
              case 
imagepng($thumbnail,$mdir.$file_thumb); break;
            }
          }
          switch (
$pictype) {
            case 
imagegif($thumbnail); break;
            case 
imagejpeg($thumbnail); break;
            case 
imagepng($thumbnail); break;
          }
        }
  } else {
    
// thumbnail file already exists, return it to browser
    
list(,,$pictype) = getimagesize($mdir.$file_thumb);
        switch (
$pictype) {
          case 
$img imagecreatefromgif($mdir.$file_thumb); break;
          case 
$img imagecreatefromjpeg($mdir.$file_thumb); break;
          case 
$img imagecreatefrompng($mdir.$file_thumb); break;
        }
        switch (
$pictype) {
          case 
imagegif($img); break;
          case 
imagejpeg($img); break;
          case 
imagepng($img); break;
        }
  }
}


// read the list of image files in the specified directory
function read_data($data_dir,$ext
{
  
$dir_handle = @opendir($data_dir);
  if (
$dir_handle) {
    while (
$file readdir($dir_handle)) {
          if (
eregi($ext,$file)) {
        
$files[] = $file;
          }
    }
    
closedir($dir_handle);
  }
  if (
gettype($files) == "array") {
    
sort($files);
  } else {
    
$files false;
  }
  return 
$files;
}


// prints out the HTML for the index page.  thumbnail images are calls to self
// with cmd=min
function index_print($dir,$ext,$template,$cols,$maxperpage,$start,$w,$h
{
  
$lines file($template);
  
$line implode("",$lines);
  
$selflink $GLOBALS["PHP_SELF"];
  
$images read_data($dir,$ext);
  if (
$images) {
    
$table $GLOBALS["infocaption"]."<br><br><table align=\"center\" cellspacing=\"10\" border=\"0\" width=\"90%\">\r\n<tr>";
    
$max count($images)-1;
    
$end $start+$maxperpage-1;
    
$counter 0;
    while (list(
$key,$image) = each($images)) {
      if ((
$key >= $start) && ($key <= $end)) {
        
$piclink $PHP_SELF."?cmd=min&pic=".$image;
            if (
$f) {$piclink .= "&f=1"; }
        
$biglink $PHP_SELF."?cmd=max&start=".$start."&pic=".$image;
        
$table .= "<td><a href=\"$biglink\"><img width=\"$w\" height=\"$h\" src=\"$piclink\" style=\"border: 1px solid black;\"></a></td>";
            
$counter++;
            if ((((
$counter) % $cols) == 0) && ($key $end)) {
               
$table .= "</tr><tr>";
            }
          }
    }
    
$table .= "</tr></table>";
    
    
// print the page numbers at the bottom
    
for ($i 0$i sizeof($images)/$maxperpage$i++) {
        
$page $i 1;
        
$nstart $i $maxperpage;
        
// if current page, then no need to put <a href    .. >
        
if ($nstart == $start) {
            
$table .= "<b>Page $page</b> - ";
        } else {
            
$table .= "<a href=\"$selflink?start=$nstart\">Page $page</a> - ";
        }    
    }
  } else {
    
$table "No files in <b>$dir</b>";
  }
  
$line str_replace("###pictable###",$table,$line);
  return 
$line;
}


// prints the HTML for full size image page    with appropriate links
function image_print($dir,$pic,$template,$start,$ext
{
  
// find the next and previous images 
  
$image_arr read_data($dir,$ext);
  
$prev "?cmd=max&start=$start&pic=";
  
$next "?cmd=max&start=$start&pic=";

  for (
$i 0$i sizeof($image_arr); $i++) {
     if (
$image_arr[$i] == $pic) {
    if (
$i == 0) {
       
$prev "";
    } else {
       
$prev .= $image_arr[$i-1];
    }
    if (
$i == (sizeof($image_arr)-1)) {
       
$next "";
    } else {
       
$next .= $image_arr[$i+1];
    }
        break;
     }
  }  

  
// print out the HTML
  
$lines file($template);
  
$line implode("",$lines);
  
$selflink $GLOBALS["PHP_SELF"];
  list(,,,
$sizestr) = getimagesize($pic);
  
// patch work    to make background gray
  
$piclink "<body bgcolor=\"#cccccc\">";
  
$piclink .= "[ <a href=\"$prev\">previous</a> | ";
  
$piclink .= "<a href=\"$selflink?start=$start\">index</a>";
  
$piclink .= " | <a href=\"$next\">next</a> ]<br><br>";
  
$src=$dir.$pic;
  
$piclink .= "<img src=\"$src\" border=\"1\" $sizestr><br><br>";
  
$line str_replace("###pictable###",$piclink,$line);
  return 
$line;
}


// routes commands based on the 'cmd' parameter
//   min - returns a thumbnail
//   max - returns the full size image
//   prints the index otherwise (default)
function main($cmd,$dir,$pic,$template,$cols,$save,$mdir,$w,$h,$maxperpage,$start,$ext=".jpg"
{
  switch (
$cmd) {
     case 
"min" :
          
thumb_print($dir,$pic,$save,$mdir,$w,$h);
          break;
     case 
"max" :
          echo 
image_print($dir,$pic,$template,$start,$ext);
          break;
      default :
          echo 
"<html><head><title>Sumit Birla: Photo Album: ";    
          echo 
$GLOBALS["REQUEST_URI"]."</title>";
          
// you may comment out the includes.  this works only for my setup
          
include("../../header.inc");
          echo 
index_print($dir,$ext,$template,$cols,$maxperpage,$start,$w,$h);
          include(
"../../footer.inc");
  }
}


main($cmd,$img_dir,$pic,$templatefile,$columns,$save,$thumbs_dir,$thumb_width,$thumb_height,$maxperpage,$start,$ext_pattern);
?>