Location: PHPKode > scripts > Image Gallery with auto-scan > image-gallery-with-auto-scan/Gallery.php
<?php

/*

  copyright by Philipp Scherb
  created January 2008
  
  hide@address.com
  NO WARRANTY, FEEL FREE TO USE (AT YOUR OWN RISK).
  
  Last Mod 29.06.2010
*/


require("GalleryUtils.php");

// counts the pic in a row.
$uPicsInRow = 0;
// number of current picture.
$curPicNum = 0;
// defines the page-number of the page to show, if the Page-System is active.
$PageToShow = 0;
// List with all files of the gallery.
$FileList = array();


// Concats the filename with the base path.
function GalleryFileName($FileName) {
  global $gal_RelDirName;
  return ($gal_RelDirName . RemoveCurDir($FileName));
}


// Shows a Thumbnail of an image
function ShowGalleryFile($Filename) {
  global $uPicsInRow;
  global $gal_DirName;
  global $gal_LinkPics;
  global $gal_PicDivClass; 
  global $gal_ScriptDir;
  global $gal_RelDirName;
  global $gal_ThumbSize;
  global $gal_ShowComments;
  global $gal_PicCommentClass;
  global $gal_PagesActive;
  global $gal_PicsPerPage;
  global $gal_LargePicDir;
  global $PageToShow;
  global $curPicNum;
  global $newImgWidth;
  global $newImgHeight;

  // page to show?
  $PageMatch = ($curPicNum <= ($gal_PicsPerPage * ($PageToShow))) &&
               ($curPicNum > ($gal_PicsPerPage * ($PageToShow - 1)));
  
  $image = IsGalleryImage($Filename); 
  $maxsize = 1.1 * $gal_ThumbSize;
  
  if ($image) {
   
    if ((!$gal_PagesActive) || ($PageMatch)) { 
      if ($uPicsInRow == 0) {
        echo "  <tr>\n";
      }

      $picFileName = $gal_RelDirName . RemoveCurDir($Filename);
      $newFilename = $gal_ScriptDir . "GalleryThumb.php?gallerypicture=" . galUrlEncode($picFileName) . "&amp;" .
	                 "gallerythumbsize=$gal_ThumbSize";	        
	                 
	  if ($gal_LargePicDir != "") {
	    $LinkFileName = $gal_LargePicDir . 	    
		  RemoveStringStart($Filename, $gal_DirName);
		$LargeFilename = $LinkFileName;
		InitNewImageSize($LargeFilename, $gal_ThumbSize);
      }
	  else {
	    $LinkFileName = $Filename;
		$LargeFilename = "";	    
		InitNewImageSize($Filename, $gal_ThumbSize);
      }           
      echo "    <td style=\"vertical-align: top; width: ".$maxsize."px; " . 
	       "height: 100%;\">\n";       
	    
      echo "      <div class=\"$gal_PicDivClass\">\n";
      echo "      <center>\n";
      if ($gal_LinkPics) { echo "        <a href=\"" . galUrlEncode($LinkFileName) . "\" target=\"_blank\">\n";}
      echo "        <img src=\"$newFilename\" " .
	       "alt=\"image\" style=\"border: 0px;\" " .
		   "width=\"$newImgWidth\" height=\"$newImgHeight\">\n";
      if ($gal_LinkPics) { echo "        </a>\n";}
      echo "      </center>\n";      
      if ($gal_ShowComments) {
        echo "      <div class=\"$gal_PicCommentClass\">\n";
        echo "        " . GetGalleryComment($Filename, $LargeFilename) . "\n";
        echo "      </div>\n";
      }   
      echo "      </div>\n";
      echo "    </td>\n";
      $uPicsInRow++;
    }
    $curPicNum++;
    return true;
  }
  else {
    return false;
  }
}


// Scans a dir for files.
function ScanGalleryDir($DirName) {
  global $gal_SubDirs;
  global $FileList;
  global $gal_ExcludedDirNames;
  
  
  $DirParts = explode("/", $DirName . " ");
  if ($DirParts == null) {
    $DirParts = array();
    $DirParts[] = $DirName;
  }
  foreach ($gal_ExcludedDirNames  as $Excluded) {
    if (in_array($Excluded, $DirParts)) {
	  return 1;
	}   
  }

  // Open dir
  if (is_dir($DirName)) {
    if ($dh = opendir($DirName)) {
      while (($file = readdir($dh)) !== false) {

        if (($file != ".") && ($file != "..")) {
          if (is_dir($DirName . $file))  {
            if ($gal_SubDirs) {
              ScanGalleryDir($DirName . $file . "/");          
            }
          }
          else {
            if (IsGalleryImage($DirName . $file)) {          
              $FileList[] = $DirName . $file;
            }          
          }
        }
               
      } // while
      closedir($dh);
    } // if opendir
  } // if is_dir 
}


// show each gallery file.
function ShowGalleryFiles() {
  global $FileList;
  global $uPicsInRow;  
  global $gal_ThumbsPerRow;
  global $gal_ThumbsPerRow;
  global $gal_PagesActive;
  global $gal_PicsPerPage;
  global $curPicNum;
  global $PageToShow;
 
  foreach ($FileList as $file) {
    if (ShowGalleryFile($file)) {
      if ($uPicsInRow >= $gal_ThumbsPerRow) {
        echo "  </tr>\n";
        $uPicsInRow = 0;               
      }
    }
          
    // skips if the upcoming pictures belong to an other page.
    if (($gal_PagesActive) && ($curPicNum > ($gal_PicsPerPage * $PageToShow))) {
      break;
    }
  } // foreach
}


// Writes the links to the pages.
function WritePageLinks() {
  global $gal_AutoLinksToPages;
  global $gal_PagesActive;   
  global $gal_PageLabel;
  global $gal_PageLabelClass;
  global $gal_LinkGetItems;
  global $gal_PicsPerPage;
  global $gal_LinkBackText;
  global $gal_LinkNextText;
  global $gal_LinkClass;
  global $PageToShow;
  global $FileList;
  
  
  if ($gal_LinkClass != "") {
    $lnkcls = "class=\"$gal_LinkClass\"";
  }
  else {
    $lnkcls = "";
  }
  
  // Pre-Define the get items of the page link.  
  if ($gal_LinkGetItems == "") {
    $lnkGet = "?";
  }
  else {
    $lnkGet = $gal_LinkGetItems . "&amp;";
  }   
  $lnkGet  = $lnkGet . "GalleryPage=";
  
  
  if ($gal_PagesActive) {
    echo "<center>\n";
    echo "<div class=\"$gal_PageLabelClass\">\n";
    if ($gal_AutoLinksToPages) {
      if ($PageToShow > 1) {
        echo "<a href=\"$lnkGet" . ($PageToShow - 1) . "\" $lnkcls>" .
		     "$gal_LinkBackText</a>\n";
      }
    }

    echo $gal_PageLabel . $PageToShow . "\n";
    
    if ($gal_AutoLinksToPages) {
      if (($PageToShow * $gal_PicsPerPage) < count($FileList)) {
        echo "<a href=\"$lnkGet" . ($PageToShow + 1) . "\" $lnkcls>" . 
		     "$gal_LinkNextText</a>\n";
      }
    }   
    echo "</div>\n";
    echo "</center>\n";    
  }
}


// Sorts files by file date. (custom sort function for usort).
function SortByDate($File1, $File2) {
  global $gal_RelDirName;
  
  $fn1= $gal_RelDirName . RemoveCurDir($File1);
  $fn2= $gal_RelDirName . RemoveCurDir($File2);  
  return filemtime($fn2) - filemtime($fn1);
}


// Searches all files in the gallery directory.
function GetGalleryFiles() {
  global $gal_SubDirs;  
  global $gal_DirName;  
  global $gal_ReverseList;
  global $gal_SortByFileDate;
  global $gal_MaxPictureCount;
  global $FileList;
  
  $FileList = array();
  ScanGalleryDir($gal_DirName);    
  
  if ($gal_SortByFileDate) {
    usort($FileList, "SortByDate");
  }  
  
  if ($gal_ReverseList) {
    $FileList = array_reverse($FileList);
  }  
  
  if ($gal_MaxPictureCount > 0) {
    $FileList = array_slice($FileList, 0, $gal_MaxPictureCount);
  }
}


// Checks the setup for errors.
function GalleryChecks() {
  global $gal_ThumbsPerRow;
  global $gal_PicsPerPage;
  global $gal_ThumbSize;
  global $gal_PagesActive;
  global $gal_PageLabel;
  global $gal_DirName;
  global $gal_LargePicDir;

  $Error = false;
  $Texts = array();  

  if (($gal_PicsPerPage < $gal_ThumbsPerRow) && ($gal_PagesActive)){
    $Error = true;
    $Texts[] = "gal_PicsPerPage must be greater than gal_ThumbsPerRow!";
  }  
  if ($gal_ThumbsPerRow <= 0) {
    $Error = true;
    $Texts[] = "gal_ThumbsPerRow must be greater than zero!";
  }
  if (($gal_PagesActive) && ($gal_PicsPerPage <= 0)) {
    $Error = true;
    $Texts[] = "gal_PagesActive must be greater than zero!";
  }
  if ($gal_ThumbSize <= 0) {
    $Error = true;
    $Texts[] = "gal_ThumbSize must be greater than zero!";
  }
  if (($gal_PagesActive) && ($gal_PageLabel == "")) {
    $Error = true;
    $Texts[] = "gal_PageLabel must be defined!";
  }
  if (!file_exists($gal_DirName)) {
    $Error = true;
    $Texts[] = "The directory \"$gal_DirName\" (gal_DirName) could not be found!";
  }  
  if (strlen($gal_DirName) > 0) {
    if ($gal_DirName[strlen($gal_DirName) - 1] != "/") {
      $Error = true;
      $Texts[] = "The directory \"$gal_DirName\" (gal_DirName) must end with \"/\" !"; 
    }
  }   
  if (($gal_LargePicDir != "") && (!file_exists($gal_LargePicDir))) {
    $Error = true;
    $Texts[] = "The directory \"$gal_LargePicDir\"  (gal_LargePicDir) " .
	           "could not be found!";
  }   

  // Echo of the errors. 
  if ($Error) {
    echo "<div class=\"GalleryError\">\n";
    echo "Error in GallerySetup-values:<br>\n";
    echo "<ol>\n";
    foreach ($Texts as $item) {
      echo "<li>\n";
      echo $item;
      echo "</li>\n";
    } 
    echo "</ol>\n";
    echo "</div>\n";  
  }

  return (!$Error);
}


// shows the picture gallery for the current setup.
function ShowGallery() {
  global $uPicsInRow;  
  global $gal_TableClass;
  global $curPicNum;

  if (!GalleryChecks()) { return 1; }

  // Search all files.
  GetGalleryFiles();
   
  $uPicsInRow = 0; 
  $curPicNum = 1;
  
  WritePageLinks();
  echo "<table class=\"$gal_TableClass\">\n";
  ShowGalleryFiles();
  echo "</table>\n"; 
  WritePageLinks();
}


// gets the page number.
$PageToShow = $_GET['GalleryPage'];
if ($PageToShow == 0) {
  $PageToShow = 1;
}


?>
Return current item: Image Gallery with auto-scan