3 define('THUMB_SIZE', 100);
5 function getPreview($imgFile, $maxSize = THUMB_SIZE)
7 # example: data/myalbum/100.mypic.jpg
8 $newImgFile = "data/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
10 if (! is_file($newImgFile))
12 $img = imagecreatefromjpeg($imgFile);
16 # don't do anything if the image is already small
17 if ($w <= $maxSize and $h <= $maxSize) {
22 # create the thumbs directory recursively
23 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
27 $newH = $h/($w/$maxSize);
29 $newW = $w/($h/$maxSize);
33 $newImg = imagecreatetruecolor($newW, $newH);
35 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
37 imagejpeg($newImg, $newImgFile);
40 imagedestroy($newImg);
43 return dirname($_SERVER["SCRIPT_NAME"])."/".$newImgFile;
46 function getAlbumPreview($dir)
48 foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
49 if (strtolower(substr($file, -4)) == ".jpg")
50 return getPreview("$dir/$file");
56 $scriptUrlPath = substr($_SERVER["SCRIPT_NAME"], 0, -4); // trim .php
58 // if url == http://localhost/photos/index/toto/titi, path_info == /toto/titi
59 // if url == http://localhost/photos/index, path_info is not set
60 // if url == http://localhost/photos/, path_info is not set
61 // if path_info is not set, we are at top level, so we redirect to /photos/index/
62 if (! isset($_SERVER["PATH_INFO"])) {
63 header("Location: $scriptUrlPath/");
67 $shortPath = $_SERVER["PATH_INFO"];
68 if ($shortPath == '/') $shortPath = '';
69 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
70 if (strpos($shortPath, '..') !== false) die(".. found in url");
73 $imageFiles = array();
74 $otherFiles = array();
76 $realDir = "images$shortPath";
78 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
80 if (is_dir("$realDir/$file"))
82 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
86 $ext = strtolower(substr($file, -4));
88 $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => dirname($scriptUrlPath)."/view/$shortPath/$file" );
90 $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
94 if (dirname($shortPath) !== '')
95 $parentLink = $scriptUrlPath.dirname($shortPath);
101 ///// template starts here /////
102 header('Content-Type: text/html; charset=utf-8');
103 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
107 <style type="text/css">
115 text-decoration: none;
118 display: inline-block;
121 width: <?php echo THUMB_SIZE ?>px;
122 height: <?php echo THUMB_SIZE ?>px;
125 vertical-align: middle;
128 height: <?php echo THUMB_SIZE ?>px;
130 vertical-align: middle;
143 <?php if ($parentLink !== '') { ?>
144 <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
147 <?php foreach($folders as $folder) { ?>
149 <?php if ($folder["preview"] === "") { ?>
150 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
152 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
153 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
158 <?php foreach ($imageFiles as $file) { ?>
159 <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>
162 <?php foreach ($otherFiles as $file) { ?>
163 <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>