3 define('THUMB_SIZE', 100);
4 define('DATA_DIR', 'data');
5 define('IMAGES_DIR', 'images');
7 function getPreview($imgFile, $maxSize = THUMB_SIZE)
9 # example: data/myalbum/100.mypic.jpg
10 $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
12 if (! is_file($newImgFile))
14 $ext = strtolower(substr($imgFile, -4));
16 $img = imagecreatefromjpeg($imgFile);
18 $img = imagecreatefrompng($imgFile);
22 # don't do anything if the image is already small
23 if ($w <= $maxSize and $h <= $maxSize) {
28 # create the thumbs directory recursively
29 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
33 $newH = $h/($w/$maxSize);
35 $newW = $w/($h/$maxSize);
39 $newImg = imagecreatetruecolor($newW, $newH);
41 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
44 imagejpeg($newImg, $newImgFile);
46 imagepng($newImg, $newImgFile);
49 imagedestroy($newImg);
52 return dirname($_SERVER["SCRIPT_NAME"])."/".$newImgFile;
55 function getAlbumPreview($dir)
57 foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
58 $ext = strtolower(substr($file, -4));
59 if ($ext == ".jpg" or $ext == ".png")
60 return getPreview("$dir/$file");
66 $scriptUrlPath = $_SERVER["SCRIPT_NAME"];
68 // if url == http://localhost/photos/index/toto/titi, path_info == /toto/titi
69 // if url == http://localhost/photos/index, path_info is not set
70 // if url == http://localhost/photos/, path_info is not set
71 // if path_info is not set, we are at top level, so we redirect to /photos/index/
72 if (! isset($_SERVER["PATH_INFO"])) {
73 header("Location: $scriptUrlPath/");
77 $shortPath = $_SERVER["PATH_INFO"];
78 if ($shortPath == '/') $shortPath = '';
79 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
80 if (strpos($shortPath, '..') !== false) die(".. found in url");
83 $imageFiles = array();
84 $otherFiles = array();
86 $realDir = IMAGES_DIR.$shortPath;
88 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
90 if (is_dir("$realDir/$file"))
92 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
96 $ext = strtolower(substr($file, -4));
97 if ($ext == ".jpg" or $ext == ".png")
98 $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => dirname($scriptUrlPath)."/view.php$shortPath/$file" );
100 $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
104 if (dirname($shortPath) !== '')
105 $parentLink = $scriptUrlPath.dirname($shortPath);
111 ///// template starts here /////
112 header('Content-Type: text/html; charset=utf-8');
113 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
117 <style type="text/css">
125 text-decoration: none;
128 display: inline-block;
131 width: <?php echo THUMB_SIZE ?>px;
132 height: <?php echo THUMB_SIZE ?>px;
135 vertical-align: middle;
138 height: <?php echo THUMB_SIZE ?>px;
140 vertical-align: middle;
153 <?php if ($parentLink !== '') { ?>
154 <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
157 <?php foreach($folders as $folder) { ?>
159 <?php if ($folder["preview"] === "") { ?>
160 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
162 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
163 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
168 <?php foreach ($imageFiles as $file) { ?>
169 <div class="square"><div class="image"><a href="<?php echo $file["link"] ?>"><img src="<?php echo $file["url"] ?>" alt="<?php echo $file["name"] ?>" /></a></div></div>
172 <?php foreach ($otherFiles as $file) { ?>
173 <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>