< Back to Code Samples
<?php 
// These scripts are necessary for the lightbox.
echo '<script type="text/javascript" src="js/prototype.js"></script><script type="text/javascript" src="js/scriptaculous.js?load=effects"></script><script type="text/javascript" src="js/lightbox.js"></script>';

// home for the galleries
$_URL_HOME "/galleries";
// home for the files
$_FILE_HOME "/home/stphotog/www" $_URL_HOME;

// for creation of thumbnails
require_once('thumbnail.php');
// for directory traversal
require_once('directory.class.php');

/**************************
    FUNCTION:  getGalleries("current")
    ARGUMENTS: $set - which set to get the galleries from
    RETURNS: nothing - output only
    PURPOSE: This function runs through the specified set folder,
        looks for all subdirectories, and lists them. It is assumed
        that there will only be galleries as subdirectories under the
        set folder.
***************************/
function getGalleries($set "current")
{
    
// Access the global homes
    
global $_FILE_HOME;
    global 
$_URL_HOME;

    
// Strip off trailing slashes
    
while (substr($set,0,1) == "/") { $set substr($set,1); }

    
// Get the directory listing for the $set variable
    
$setDir = new directory_obj($_FILE_HOME "/" $set);
    
    
// Did it work?
    
if (!is_object($setDir)) { echo "not object"; exit(); }
    
    
// Now get a listing of the galleries
    
$galleries $setDir->getSubDirs();
    
    
// If there weren't any galleries, provide useful feedback
    
if (!$galleries) { echo "Coming soon..."; return; }

    
// Otherwise, iterate through the galleries    
    
foreach($galleries as $gallery) {

        
// Get all the files from the gallery
        
$files $gallery->getFiles("jpg");

        
// This will be used to count how many files there are.
        
$filecount 0;

        
// Iterate through the files
        
foreach ($files as $file => $size){
            
// If it's a thumbnail or the gallery icon, ignore it. Otherwise count it
            
if (substr($file, -7) == "_tn.jpg" || $file == "gallery_icon.jpg") { continue; }
            else { 
$filecount++; }
        }
        
        
// If there were no files, skip the gallery
        
if ($filecount == 0) { continue; }
        
        
// If this gallery has a gallery_icon already created, then use it as the 
        // gallery icon. Otherwise, use the default one.
        
$thumb = (array_key_exists("gallery_icon.jpg",$gallery->getFiles("jpg"))) ?
            
"$_URL_HOME/{$setDir->name()}/{$gallery->name()}/gallery_icon.jpg" :
            
"/img/gallery_icon.png";
        
        
// Output the results from this gallery
        
echo "<div class=\"setThumb\">
                <a href=\"main.php?section=viewGallery&set=$set&gallery={$gallery->name()}&\">
                    <img src=\"$thumb\" >\r
                </a>\r
                <span class=\"label\">\r (" 
$filecount ." photos)</span>\r
            </div>"
;
    }
}

/**************************
    FUNCTION:  buildGallery("current/gallery_I")
    ARGUMENTS: $theGallery - which gallery to build
    RETURNS: output only
    PURPOSE: This function iterates through the gallery folder, grabs all the
        images and displays their thumbnails with links to the lightbox'd normal
        image. If there isn't a thumbnail for it, it creates one. Handy!
***************************/
function buildGallery($theGallery "current/gallery_I")
{
    
// Access the globals
    
global $_FILE_HOME;
    global 
$_URL_HOME
    
    
// Get the listing of images in this gallery
    
$dirlist = new directory_obj($_FILE_HOME "/" $theGallery);

    
$images = array();

    
// Iterate through all jpg's in this directory    
    
foreach ($dirlist->getFiles("jpg") as $file => $size){
        
// If the file isn't a thumbnail or the gallery icon, then count it.
        // Note that even though it's skipping over _tn's, it's still storing the
        // appropriate _tn filenames for each image. This is used later.
        
if (strtolower(substr($file,-7)) != '_tn.jpg' && $file != "gallery_icon.jpg") {
            
$images[$file]["size"] = $size;
            
$images[$file]["thumb"] = substr($file,0,-4) . "_tn.jpg";
        }
    }

    
// Iterate through the images we parsed out. $props contains "size" and "thumb"
    
foreach($images as $image => $props)
    {
        
// If there was a thumbnail already, it would have been in the directory listing
        
if (!array_key_exists($props["thumb"],$dirlist->getFiles("jpg")))
        
// If *NOT*, then create it.
        
create_thumb($image,$_FILE_HOME."/".$theGallery."/",75); }
        
// Output the image HTML. rel=lightbox[gallery] activates the lightbox for this
        // image.
        
echo "<a href=\"$_URL_HOME/$theGallery/$image\" rel=\"lightbox[gallery]\"><img src=\"$_URL_HOME/$theGallery/" $props["thumb"] . "\" class=\"galleryThumb\"/></a>";
    }
}

?>