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