Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Creating an Image Gallery
MISCELLANEOUS

Creating an Image Gallery
By: notepad
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2003-10-24

    Table of Contents:
  • Creating an Image Gallery
  • getting started
  • moving along
  • review and organize
  • thumbnails
  • conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Creating an Image Gallery - review and organize


    (Page 4 of 6 )

    the first script we covered was config.php

    <?php

    // main images directory
    define('PATH', '/inetpub/wwwroot/scripts/gallery/images/');

    // valid file mime types, all other files will be ignored
    define('TYPE', serialize(array('image/jpg', 'image/jpeg', 'image/pjpeg')));

    // how many rows of images to display per page
    define('ROWS', 3);

    // how many columns of images to display per page
    define('COLS', 5);

    // maximum thumbnail width
    define('THMBWIDTH', 100);

    // maximum thumbnail height
    define('THMBHEIGHT', 100);

    ?>

    the second script we covered, images.php, was built step by step. the following is what the code should look like as a whole, you'll notice i've stripped out a lot of the comments and added the starting/ending table tags.

    <?php

    require_once('config.php');

    // grab our navigation
    $nav = isset($_REQUEST['nav']) ? urldecode($_REQUEST['nav']) : null;
    if(isset($nav))
    {
        $crunch = explode('>', trim($nav));
        foreach($crunch as $value)
        {
           // IMPORTANT! validate for teh h4x0rz
        if(!ctype_alnum(str_replace(' ', '', $value)))
            $invalid = true;
        }
        if($invalid)
        $nav = null;
    }

    // decide what page of a paticular gallery we're viewing
    $pg = isset($_REQUEST['pg']) && (ctype_digit($_REQUEST['pg']) || $_REQUEST['pg'] == 'all') ? $_REQUEST['pg'] : 0;
    $viewall = is_numeric($pg) ? null : true;
    $pg = intval($pg) <= 0 ? 1 : $pg;

    // images per page
    $section = ROWS * COLS;
    // last possible ending point for images, depending which page we're on
    $end = $section * $pg;
    // starting point for images, depending which page we're on
    $start = $end - $section;
    // previous page
    $prev = $pg - 1;
    // next page
    $next = $pg + 1;

    // start table
    print('<table border="0" align="center" cellpadding="1" cellspacing="1">');
        
    // draw previous and next links
    function navigator($total)
    {
        $p = '<a href="'.$_SERVER['PHP_SELF']
           .'?nav='.rawurlencode($GLOBALS['nav'])
           .'&pg='.$GLOBALS['prev'].'"><<</a>';
        $n = '<a href="'.$_SERVER['PHP_SELF']
           .'?nav='.rawurlencode($GLOBALS['nav'])
           .'&pg='.$GLOBALS['next'].'">>></a>';
        $a = '<a href="'.$_SERVER['PHP_SELF']
           .'?nav='.rawurlencode($GLOBALS['nav'])
           .'&pg=all">view all</a>';
        
        // the navigation
        print('<tr><td align="center" colspan="' . COLS . '">');
        
        if(($GLOBALS['pg'] == 1 && $GLOBALS['end'] >= $total) || isset($GLOBALS['viewall']))
        print('<< : view all : >>');
        elseif($GLOBALS['pg'] == 1)
        print('<< : '.$a.' : '.$n);
        elseif($GLOBALS['end'] < $total)
        print($p.' : '.$a.' : '.$n);
        else
        print($p.' : '.$a.' : >>');
        
        print('</td></tr>');
    }

    // get directory tree
    function directory($dir)
    {
        $mydir = opendir($dir);
        while(false !== ($file = readdir($mydir)))
        {
        if($file != "." && $file != "..")
        {
            if(is_dir($dir.$file))
            {
                chdir('.');
            $tree[$file] = directory($dir.$file.'/');
            chdir('..');
            }
            else
            {
            $size = getimagesize($dir.$file);
            if(in_array($size['mime'], unserialize(TYPE)))
                $tree[] = $file;
            }
        }
        }
        closedir($mydir);
        return $tree;
    }
    $tree = directory(PATH);

    // display category links and images
    function display_body($navtree)
    {
        if(isset($GLOBALS['nav']))
        {
        // we'll need the path as a directory for displaying images
        for($i=1; $i<count($GLOBALS['crunch']); $i++)
            $uri .= !is_numeric($GLOBALS['crunch'][$i]) ? $GLOBALS['crunch'][$i].'/' : '';
        }
        
        if(is_array($navtree))
        {
        if(!is_numeric(implode('', array_keys($navtree))))
        {
            print('<tr><td>');
            foreach($navtree as $key=>$value)
            {
            if(!is_int($key))
            {
                if(is_array($value))
                print('<a href="'.$_SERVER['PHP_SELF']
                     .'?nav='.rawurlencode($GLOBALS['nav']
                     .'>'.$key).'">'.$key.'</a><br />');
                else
                print($key.'<br />');
            }
            }
            print('</td></tr>');
        }
        else
        {
            // decide how many rows to display
            $total = count($navtree);
            if(isset($GLOBALS['viewall']))
            {
            $check = $total / COLS;
            $rows = is_int($check) ? $check : floor($check) + 1;
            }
            else
            $rows = ROWS;

            navigator($total);
            for($i=0; $i<$rows; $i++)
            {
            print('<tr>');
            for($n=0; $n<COLS; $n++)
            {
                $index = $GLOBALS['start']++;
                $image = $index < $total ? $navtree[$index] : ' ';
                print('<td align="center" width="'.THMBWIDTH.'" height="'.THMBHEIGHT.'">');
                if($image != ' ')
                {
                print('<a href="'.$_SERVER['PHP_SELF']
                     .'?nav='.rawurlencode($GLOBALS['nav']
                     .'>'.$index).'">');
                print('<img src="imgsrc.php?src='
                     .PATH.rawurlencode($uri).$image.'" border="0" />');
                print('</a>');
                }
                else
                print($image);
                
                print('</td>');
            }
            print('</tr>');
            }
            navigator($total);
        }
        }
        elseif(file_exists(PATH.$uri.$navtree))
        {
        $root = explode('/', dirname(PATH.'.'));
        print('<tr><td><img src="'.end($root).'/'.$uri.$navtree.'"></td></tr>');
        }
        else
        print('<tr><td>invalid request</td></tr>');
    }

    // navigate appropriate categories
    function categories($navtree)
    {
        if(isset($GLOBALS['nav']))
        {
        for($i=1; $i<count($GLOBALS['crunch']); $i++)
            $navtree = &$navtree[$GLOBALS['crunch'][$i]];

        display_body($navtree);
        }
        else
        display_body($navtree);
    }
    categories($tree);

    // end table
    print('</table>');

    ?>

    More Miscellaneous Articles
    More By notepad

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap