e6ab0c1e2e1c5b5b94ce80b5d87292203e907171
[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 (mime_content_type("$dir/$file") == "image/jpeg")
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 $imageFiles = array();
86 $otherFiles = array();
87
88 $realDir = "images$shortPath";
89
90 foreach (scandir($realDir) as $file) if ($file != '.')
91 {
92         if ($file == '..')
93         {
94                 echo "<div><a href=\"$scriptUrlPath".dirname($shortPath)."/\">..</a></div>\n";
95         }
96         elseif (is_dir("$realDir/$file"))
97         {
98                 echo "<div>";
99                 $preview = getAlbumPreview("$realDir/$file");
100                 if ($preview !== '') {
101                         echo "<img src=\"$preview\" /> ";
102                 }
103
104                 echo "<a href=\"$scriptUrlPath$shortPath/$file\">$file</a>";
105                 echo "</div>\n";
106         }
107         else
108         {
109                 $mime = mime_content_type("$realDir/$file");
110
111                 if ($mime == "image/jpeg")
112                         $imageFiles[] = $file;
113                 else
114                         $otherFiles[] = $file;
115         }
116 }
117
118 foreach ($imageFiles as $file) {
119         echo "<div class=\"square\"><div class=\"image\"><a href=\"".dirname($scriptUrlPath)."/view/$shortPath/$file\"><img src=\"".getPreview("$realDir/$file", 100)."\" /></a></div></div>\n";
120 }
121
122 foreach ($otherFiles as $file) {
123         echo "<div><a href=\"".dirname($scriptUrlPath)."/$realDir/$file\">$file</a></div>\n";
124 }
125
126 ?>
127
128 </body>
129 </html>