1<?php
2//////////////////////////////////////////////////////////////
3//   phpThumb() by James Heinrich <info@silisoftware.com>   //
4//        available at http://phpthumb.sourceforge.net      //
5//         and/or https://github.com/JamesHeinrich/phpThumb //
6//////////////////////////////////////////////////////////////
7///                                                         //
8// phpThumb.demo.gallery.php                                //
9// James Heinrich <info@silisoftware.com>                   //
10//                                                          //
11// Demo showing basic usage of phpThumb in a photo gallery  //
12//                                                          //
13//////////////////////////////////////////////////////////////
14die('For security reasons, this demo is disabled by default. Please comment out line '.__LINE__.' in '.basename(__FILE__));
15?>
16<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
17<html>
18<head>
19	<title>phpThumb :: sample photo gallery demo</title>
20</head>
21<body>
22This is a demo of how you can use <a href="http://phpthumb.sourceforge.net">phpThumb()</a> in an image gallery.<br>
23<hr>
24<?php
25$docroot = realpath((getenv('DOCUMENT_ROOT') && preg_match('#^'.preg_quote(realpath(getenv('DOCUMENT_ROOT'))).'#', realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', str_replace(DIRECTORY_SEPARATOR, '/',  __DIR__ )));
26$imgdir = '/images/';                        // webroot-relative path to main images directory (only this and subdirectories of this will be displayed)
27$thumb  = '/phpThumb.php';                   // webroot-relative path to "phpThumb.php"
28$config = '/phpThumb.config.php';            // webroot-relative path to "phpThumb.php"
29$popup  = '/demo/phpThumb.demo.showpic.php'; // webroot-relative path to "phpThumb.demo.showpic.php" (only used if $use_popup == true)
30$thumbnailsize = 120;                        // size of thumbnails in pixels when browsing gallery
31$displaysize   = 480;                        // size of large image display (popup or plain image) after clicking on thumbnail
32$use_popup     = true;                       // if true, open large image in self-resizing popup window; if false, display larger image in main window
33
34//////////////////////////////////////////////////////////////
35
36require_once $docroot.$config;
37
38$dirlimit = realpath($docroot.'/'.$imgdir);
39
40$captionfile = $docroot.'/'.$imgdir.(@$_REQUEST['dir'] ? $_REQUEST['dir'].'/' : '').'captions.txt';
41if (file_exists($captionfile)) {
42	$filecontents = file($captionfile);
43	foreach ($filecontents as $key => $value) {
44		@list($photo, $caption) = explode("\t", $value);
45		$CAPTIONS[$photo] = $caption;
46	}
47}
48
49if (!empty($_REQUEST['pic'])) {
50
51	$alt = @$CAPTIONS[$_REQUEST['pic']] ? $CAPTIONS[$_REQUEST['pic']] : $_REQUEST['pic'];
52	echo '<img src="'.htmlentities(phpThumbURL('src='.urlencode($imgdir.@$_REQUEST['dir'].'/'.$_REQUEST['pic']).'&w='.$displaysize.'&h='.$displaysize, $thumb)).'" border="0" alt="'.htmlentities($alt, ENT_QUOTES).'"><br>';
53	echo '<div align="center">'.htmlentities(@$CAPTIONS[$_REQUEST['pic']]).'</div>';
54
55} else {
56
57	$currentdir = realpath($docroot.'/'.$imgdir.@$_REQUEST['dir']);
58	if (!preg_match('#^'.preg_quote($dirlimit).'#', $currentdir)) {
59		echo 'Cannot browse to "'.htmlentities($currentdir).'"<br>';
60	} elseif ($dh = @opendir($currentdir)) {
61		$folders = array();
62		$pictures = array();
63		while ($file = readdir($dh)) {
64			if (is_dir($currentdir.'/'.$file) && ($file[0] != '.')) {
65				$folders[] = $file;
66			} elseif (preg_match('#\\.(jpe?g|gif|png|bmp|tiff?)$#i', $file)) {
67				$pictures[] = $file;
68			}
69		}
70		closedir($dh);
71		if (preg_match('#^'.preg_quote($dirlimit).'#', realpath($currentdir.'/..'))) {
72			echo '<a href="'.htmlentities($_SERVER['PHP_SELF'].'?dir='.urlencode($_REQUEST['dir'].'/..'), ENT_QUOTES).'">Parent directory</a><br>';
73		}
74		if (!empty($folders)) {
75			echo '<ul>';
76			rsort($folders);
77			foreach ($folders as $dummy => $folder) {
78				echo '<li><a href="'.htmlentities($_SERVER['PHP_SELF'].'?dir='.urlencode(@$_REQUEST['dir'].'/'.$folder), ENT_QUOTES).'">'.htmlentities($folder).'</a></li>';
79			}
80			echo '</ul>';
81		}
82		if (!empty($pictures)) {
83			foreach ($pictures as $file) {
84				$alt = (!empty($CAPTIONS[$file]) ? $CAPTIONS[$file] : $file);
85				echo '<table style="float: left;">'.(!empty($CAPTIONS[$file]) ? '<caption align="bottom">'.htmlentities($CAPTIONS[$file]).'</caption>' : '').'<tbody><tr><td>';
86				if ($use_popup) {
87					echo '<a title="'.htmlentities($alt, ENT_QUOTES).'" href="#" onClick="window.open(\''.$popup.'?src='.htmlentities($imgdir.@$_REQUEST['dir'].'/'.$file.'&w='.$displaysize.'&h='.$displaysize.'&title='.urlencode(@$CAPTIONS[$file] ? $CAPTIONS[$file] : $file)).'\', \'showpic\', \'width='.$displaysize.',height='.$displaysize.',resizable=no,status=no,menubar=no,toolbar=no,scrollbars=no\'); return false;">';
88				} else {
89					echo '<a title="'.htmlentities($alt, ENT_QUOTES).'" href="'.$_SERVER['PHP_SELF'].'?dir='.htmlentities(urlencode(@$_REQUEST['dir']).'&pic='.urlencode($file)).'">';
90				}
91				echo '<img src="'.htmlentities(phpThumbURL('src='.urlencode($imgdir.@$_REQUEST['dir'].'/'.$file).'&zc=1&w='.$thumbnailsize.'&h='.$thumbnailsize, $thumb)).'" border="1" width="'.$thumbnailsize.'" height="'.$thumbnailsize.'" alt="'.htmlentities($alt, ENT_QUOTES).'">';
92				echo '</a></td></tr></tbody></table>';
93			}
94			echo '<br clear="all">';
95		} else {
96			echo '<i>No pictures in "'.htmlentities(str_replace(realpath($docroot), '', realpath($docroot.'/'.$imgdir.@$_REQUEST['dir']))).'"</i>';
97		}
98	} else {
99		echo 'failed to open "'.htmlentities($currentdir).'"';
100	}
101
102}
103?>
104</body>
105</html>
106