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 $ext = strtolower(substr($file, -4));
14 $img = imagecreatefromjpeg($imgFile);
16 $img = imagecreatefrompng($imgFile);
20 # don't do anything if the image is already small
21 if ($w <= $maxSize and $h <= $maxSize) {
26 # create the thumbs directory recursively
27 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
31 $newH = $h/($w/$maxSize);
33 $newW = $w/($h/$maxSize);
37 $newImg = imagecreatetruecolor($newW, $newH);
39 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
42 imagejpeg($newImg, $newImgFile);
44 imagepng($newImg, $newImgFile);
47 imagedestroy($newImg);
50 return dirname($_SERVER["SCRIPT_NAME"])."/".$newImgFile;
53 function getAlbumPreview($dir)
55 foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
56 $ext = strtolower(substr($file, -4));
57 if ($ext == ".jpg" or $ext == ".png")
58 return getPreview("$dir/$file");
64 $scriptUrlPath = substr($_SERVER["SCRIPT_NAME"], 0, -4); // trim .php
66 // if url == http://localhost/photos/index/toto/titi, path_info == /toto/titi
67 // if url == http://localhost/photos/index, path_info is not set
68 // if url == http://localhost/photos/, path_info is not set
69 // if path_info is not set, we are at top level, so we redirect to /photos/index/
70 if (! isset($_SERVER["PATH_INFO"])) {
71 header("Location: $scriptUrlPath/");
75 $shortPath = $_SERVER["PATH_INFO"];
76 if ($shortPath == '/') $shortPath = '';
77 // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
78 if (strpos($shortPath, '..') !== false) die(".. found in url");
81 $imageFiles = array();
82 $otherFiles = array();
84 $realDir = "images$shortPath";
86 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
88 if (is_dir("$realDir/$file"))
90 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
94 $ext = strtolower(substr($file, -4));
95 if ($ext == ".jpg" or $ext == ".png")
96 $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => dirname($scriptUrlPath)."/view/$shortPath/$file" );
98 $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
102 if (dirname($shortPath) !== '')
103 $parentLink = $scriptUrlPath.dirname($shortPath);
109 ///// template starts here /////
110 header('Content-Type: text/html; charset=utf-8');
111 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
115 <style type="text/css">
123 text-decoration: none;
126 display: inline-block;
129 width: <?php echo THUMB_SIZE ?>px;
130 height: <?php echo THUMB_SIZE ?>px;
133 vertical-align: middle;
136 height: <?php echo THUMB_SIZE ?>px;
138 vertical-align: middle;
151 <?php if ($parentLink !== '') { ?>
152 <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
155 <?php foreach($folders as $folder) { ?>
157 <?php if ($folder["preview"] === "") { ?>
158 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
160 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
161 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
166 <?php foreach ($imageFiles as $file) { ?>
167 <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>
170 <?php foreach ($otherFiles as $file) { ?>
171 <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>