css tunning for up link and folders
[bizou.git] / index.php
1 <?php
2
3 define('THUMB_SIZE', 100);
4
5 function getPreview($imgFile, $maxSize = THUMB_SIZE)
6 {
7         # example: data/myalbum/100.mypic.jpg
8         $newImgFile = "data/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
9         
10         if (! is_file($newImgFile))
11         {
12                 $img = imagecreatefromjpeg($imgFile);
13
14                 $w = imagesx($img);
15                 $h = imagesy($img);
16                 # don't do anything if the image is already small
17                 if ($w <= $maxSize and $h <= $maxSize) {
18                         imagedestroy($img);
19                         return $imgFile;
20                 }
21
22                 # create the thumbs directory recursively
23                 if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
24
25                 if ($w > $h) {
26                         $newW = $maxSize;
27                         $newH = $h/($w/$maxSize);
28                 } else {
29                         $newW = $w/($h/$maxSize);
30                         $newH = $maxSize;
31                 }
32
33                 $newImg = imagecreatetruecolor($newW, $newH);
34
35                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
36
37                 imagejpeg($newImg, $newImgFile); 
38                 
39                 imagedestroy($img);
40                 imagedestroy($newImg);
41         }
42
43         return dirname($_SERVER["SCRIPT_NAME"])."/".$newImgFile;
44 }
45
46 function getAlbumPreview($dir)
47 {
48         foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') {
49                 if (strtolower(substr($file, -4)) == ".jpg")
50                         return getPreview("$dir/$file");
51         }
52
53         return '';
54 }
55
56 $scriptUrlPath = substr($_SERVER["SCRIPT_NAME"], 0, -4); // trim .php
57
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/");
64         exit();
65 }
66
67 $shortPath = $_SERVER["PATH_INFO"];
68 if ($shortPath == '/') $shortPath = '';
69
70 $folders = array();
71 $imageFiles = array();
72 $otherFiles = array();
73
74 $realDir = "images$shortPath";
75
76 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
77 {
78         if (is_dir("$realDir/$file"))
79         {
80                 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
81         }
82         else
83         {
84                 $ext = strtolower(substr($file, -4));
85                 if ($ext == ".jpg")
86                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => dirname($scriptUrlPath)."/view/$shortPath/$file" );
87                 else
88                         $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
89         }
90 }
91
92 if (dirname($shortPath) !== '')
93         $parentLink = $scriptUrlPath.dirname($shortPath);
94 else
95         $parentLink = "";
96
97 ?>
98 <?php
99 ///// template starts here /////
100 header('Content-Type: text/html; charset=utf-8');
101 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
102 ?>
103 <html>
104 <head>
105 <style type="text/css">
106 body {
107         padding-top: 2em;
108 }
109 img {
110         border: 0;
111 }
112 a {
113         text-decoration: none;
114 }
115 .square {
116         display: inline-block;
117 }
118 .image {
119         width: <?php echo THUMB_SIZE ?>px;
120         height: <?php echo THUMB_SIZE ?>px;
121         display: table-cell;
122         text-align: center;
123         vertical-align: middle;
124 }
125 .foldername {
126         height: <?php echo THUMB_SIZE ?>px;
127         display: table-cell;
128         vertical-align: middle;
129 }
130 #parentfolder {
131         position: fixed;
132         font-size: 4em;
133         font-weight: bold;
134         top: 0;
135         left: 0;
136 }
137 </style>
138 </head>
139 <body>
140
141 <?php if ($parentLink !== '') { ?>
142         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
143 <?php } ?>
144
145 <?php foreach($folders as $folder) { ?>
146         <div class="folder">
147         <?php if ($folder["preview"] === "") { ?>
148                 <a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a>
149         <?php } else { ?>
150                 <div class="square"><div class="image"><a href="<?php echo $folder["link"] ?>"><img src="<?php echo $folder["preview"] ?>" /></a></div></div>
151                 <div class="square"><div class="foldername"><a href="<?php echo $folder["link"] ?>"><?php echo $folder["name"] ?></a></div></div>
152         <?php } ?>
153         </div>
154 <?php } ?>
155
156 <?php foreach ($imageFiles as $file) { ?>
157         <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>
158 <?php } ?>
159
160 <?php foreach ($otherFiles as $file) { ?>
161         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
162 <?php } ?>
163
164 </body>
165 </html>