Zend - The PHP Company




Thumbnails

Add Code


Dynamic Gallery  

Type: application
Added by: madlogic
Entered: 21/07/2003
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 10079
This is the first version of my Dynamic Gallery page. Dynamically takes the images from given categorys in each directory of that category, and creates a thumbnail on the fly. Very useful script.


<?
//____________________________________________//
//               _ _____     _ _              //
//   _____ ___ _| |   __|___| | |___ ___ _ _  //
//  |     | .'| . |  |  | .'| | | -_|  _| | | //
//  |_|_|_|__,|___|_____|__,|_|_|___|_| |_  | //
//                                      |___| //
//____________________________________________//
/*
    PHP - M a d G a l l e r y
    Structure layout:
        + index.php
        |-- imagesdir1
          |-- gallery1
          | |-- image.jpg
          |-- gallery2
            |-- image.jpg
        
*/

// Session starter.
session_start();
session_register();

// Request query strings.
import_request_variables ("g","_");

// Configuration variables.
$imgsDir "images/";
$imgsUrl "http://website.com/images";

$siteTitle "Title - $imgsUrl";
$webMaster "Webmaster > Webmaster@Website.com";

// HTML Template.
$headerPage "
    <html>
    <head>
    <title>"
.$siteTitle." > ".$webMaster."</title>
    <!doctype html public "
-//w3c//dtd html 4.0 transitional//en">
    
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1">
    <
style>
    
A:LinkA:ActiveA:Visited {
        
color#000000;
        
font-faceVerdanaArialHelvetica;
        
font-size12px;
        
text-decorationnone
    
}
    
A:Hover {
        
color#FF0000
    
}
    
Body {
        
color#000000;
        
font-faceVerdanaArialHelvetica;
        
font-size12px;
        
text-decorationnone;
        
background-attachmentfixed;
        
background-repeatno-repeat;
        
background-positiontop left
    
}
    .
Border {
        
border-stylesolid;
        
border-width1;
        
border-color#000000
    
}
    .
Thumb {
        
border-color#000000;
        
border-stylesolid;
        
border-width1;
        
filteralpha(opacity=50)
    }
    </
style>
    </
head>
    <
body background="images/Background.gif" bgproperties="fixed">
";
$footerPage = "
    
<br><hr size="1" color="#000000" width="100%" noshade>
    
Contact: <b>".$webMaster."</b>
    </
body>
    </
html>
";
// Index page.
if (!isset (
$_cat) && !isset ($_pid)) {
    // Print header page.
    print 
$headerPage;
    // Check if img directory exists.
    if (!is_dir(
$imgsDir)) {
        // Return error message.
        die ("
            
$headerPage
            
<b>E</b>rror-> $imgsDir does not exist!
            
$footerPage
        
");
    }
    // Get images directory into array.
    
$imgsArray = array ();
    
$d = dir ("$imgsDir");
    while (false !== (
$string = $d->read())) {
        if (
$string != "." && $string != ".." && $string != "Thumbs.db" && $string != "thumbs") {
            array_push (
$imgsArray$string);
        }
    }
    
$d->close();
    // Sort array.
    sort (
$imgsArray);
    reset (
$imgsArray);
    // Print out main.
    print "
        
<font size="6">".$siteTitle."</font>
        <
hr size="1" color="#000000" width="100%" noshade>
        <
font size="3"><i>Select a gallery below to view the contents held within that desired gallery.</i></font>
        <
hr size="1" color="#000000" width="100%" noshade><br>
    
";
    // Create thumbnails.
    
$i = 0;
    foreach (
$imgsArray as $key => $value) {
        // Grab thumbnail
        
$tArray = array ();
        
$t = dir ("$imgsDir/$value");
        while (false !== (
$string = $t->read())) {
            if (
$string != "." && $string != ".." && $string != "Thumbs.db" && $string != "thumbs") {
                array_push (
$tArray$string);
            }
        }
        
$d->close();
        
$image = "$tArray[0]";
        
$src = "$imgsDir/$value/$image";
        if (!file_exists("
images/thumbs_index/$image")) {
            copy(
$src,"images/thumbs_index/$image");
            if (filesize("
images/thumbs_index/$image") > "20500") {
                exec("
D:/WebRoot/Bin/ImageMagick/mogrify.exe -resize 86x74 D:/WebRoot/Docs/wpgirls.gotdns.org/images/thumbs_index/$image");
            }
        } else {
            if (!session_is_registered("
tmp")) {
                
$indexed[$i][name] = $value;
                
$indexed[$i][url] = "?cat=$value&pid=0";
                
$indexed[$i][thumb] = "images/thumbs_index/$image";
            }
        }
        
$i++;
    }
    // Grab session if any.
    if (!session_is_registered("
tmp")) {
        session_register("
tmp");
        
$_SESSION[tmp] = $indexed;
    } else if (session_is_registered("
tmp")) {
        
$indexed = array();
        
$indexed = $_SESSION[tmp];
    }
    // Create the dynamic table.
    
$i = 0;
    
$cols = 6;
    
$rows = count($indexed) / $cols;
    print "
<table width="690">";
    for (
$r = 0; $r < $rows$r++) {
        print "
<tr>";
        for (
$c = 0; $c < $cols$c++) {
            if (file_exists("".
$indexed[$i][thumb]."")) {
                print "
<td align="center"><a href="".$indexed[$i][url]."" border="0"><img src="".$indexed[$i][thumb]."" width="86" height="74" class="Thumb" onMouseOver="this.filters.alpha.opacity='100'" onMouseOut="this.filters.alpha.opacity='50'"><br>".str_replace("_"," ","".$indexed[$i][name]."")."</a></td>";
            }
            
$i++;
        }
        print "
</tr>";
    }
    print "
</table>";

    // Print footer page.
    print "
    
<br><hr size="1" color="#000000" width="100%" noshade>
    
Their are currently <b>[</b".count($imgsArray)." <b>]</bgallerys listed.
    
".$footerPage;
}
// Gallery page.
if (isset (
$_cat)) {
    // Check for pid.
    if (isset (
$_pid)) {
        print 
$headerPage;
        // Get array from gallery.
        
$imgsArray = array ();
        
$d = dir ("$imgsDir/$_cat");
        while (false !== (
$string = $d->read())) {
            if (
$string != "." && $string != ".." && $string != "Thumbs.db" && $string != "thumbs") {
                array_push (
$imgsArray$string);
            }
        }
        
$d->close();
        // Sort array.
        sort (
$imgsArray);
        reset (
$imgsArray);
        // Return desired gallery.
        print "
            
<font size="6">".$siteTitle."</font>
            <
hr size="1" color="#000000" width="100%" noshade>
            <
font size="3"><i>Viewing gallery <b>[</b".str_replace("_"," ",$_cat)." <b>]</b></i> - <a href="javascript:void(history.go(-1))">Go Back</a></font>
            <
hr size="1" color="#000000" width="100%" noshade><br>
            <
center>
            <
a href="$imgsDir/$_cat/".$imgsArray[$_pid]."" target="_blank"><img src="$imgsDir/".$_cat."/".$imgsArray[$_pid]."" width="640" height="480" border="0" class="Border"></a><br>
            <
br><hr size="1" color="#000000" width="100%" noshade><br>
        
";
        // Grab images from array.
        
$newArray = array ();
        
$i = 0;
        foreach (
$imgsArray as $key => $value) {
            if (
$key != $_pid) { $newArray[$i] = $value; }
            
$i++;
        }
        // Return gallery.
        foreach (
$newArray as $key => $value) {
            if (!file_exists("
images/thumbs_gallery/$value")) {
                copy("
$imgsDir/$_cat/$value","images/thumbs_gallery/$value");
                if (filesize("
images/thumbs_gallery/$value") > "20500") {
                    exec("
D:/WebRoot/Bin/ImageMagick/mogrify.exe -resize 100x80 D:/WebRoot/Docs/wpgirls.gotdns.org/images/thumbs_gallery/$value");
                }
            }
            print "
<a href="?cat=$_cat&pid=$key"><img src="images/thumbs_gallery/$valuewidth="100" height="80" border="0" class="Border"></a>&nbsp;";
        }
        print "
</center>n".$footerPage;
    } else {
        // Return error message.
        die ("
            
$headerPage
            
<b>E</b>rror-> pid not found in query string ...
            
$footerPage
        
");
    }
}
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories