b5db265498d49f377ea6a7d86c13064179a8bb38
[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 $shortPath = isset($_SERVER["PATH_INFO"]) ? $_SERVER["PATH_INFO"] : "";
57 if ($shortPath == '/') $shortPath = '';
58 $scriptUrlPath = substr($_SERVER["SCRIPT_NAME"], 0, -4); // trim .php
59
60 $folders = array();
61 $imageFiles = array();
62 $otherFiles = array();
63
64 $realDir = "images$shortPath";
65
66 foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..')
67 {
68         if (is_dir("$realDir/$file"))
69         {
70                 $folders[] = array( "name" => $file, "link" => "$scriptUrlPath$shortPath/$file", "preview" => getAlbumPreview("$realDir/$file") );
71         }
72         else
73         {
74                 $ext = strtolower(substr($file, -4));
75                 if ($ext == ".jpg")
76                         $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => dirname($scriptUrlPath)."/view/$shortPath/$file" );
77                 else
78                         $otherFiles[] = array( "name" => $file, "link" => dirname($scriptUrlPath)."/$realDir/$file" );
79         }
80 }
81
82 if (dirname($shortPath) !== '')
83         $parentLink = $scriptUrlPath.dirname($shortPath);
84 else
85         $parentLink = "";
86
87 ?>
88 <?php
89 ///// template starts here /////
90 header('Content-Type: text/html; charset=utf-8');
91 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
92 ?>
93 <html>
94 <head>
95 <style type="text/css">
96 img {
97         border: 0;
98         vertical-align: middle;
99 }
100
101 .square {
102         display: inline-block;
103 }
104
105 .image {
106         width: <?php echo THUMB_SIZE ?>px;
107         height: <?php echo THUMB_SIZE ?>px;
108         display: table-cell;
109         text-align: center;
110         vertical-align: middle;
111 }
112 </style>
113 </head>
114 <body>
115
116 <?php if ($parentLink !== '') { ?>
117         <div id="parentfolder"><a href="<?php echo $parentLink ?>">^</a></div>
118 <?php } ?>
119
120 <?php foreach($folders as $folder) { ?>
121         <div class="folder">
122         <a href="<?php echo $folder["link"] ?>">
123         <?php if ($folder["preview"] !== "") { ?>
124                 <img src="<?php echo $folder["preview"] ?>" />
125         <?php } ?>
126         <?php echo $folder["name"] ?>
127         </a>
128         </div>
129 <?php } ?>
130
131 <?php foreach ($imageFiles as $file) { ?>
132         <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>
133 <?php } ?>
134
135 <?php foreach ($otherFiles as $file) { ?>
136         <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
137 <?php } ?>
138
139 </body>
140 </html>