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