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