its all about you, its all about me, its what they said we could be
[quickshow.git] / show.php
1 <?php
2
3 /* This is a very simple app
4  * - you give it a directory where it'll look for jpg's and it'll give a quick index
5  * of them along with clickable links to see the full thing
6  */
7
8 // set these variables
9 $n_per_line = 6; // number of pics in each line of the table
10 $thumb_size = 300; // size of thumbnails
11 $med_size = 1024; // size of "medium" image
12
13
14 $show_dir = "/net/iron/export/quickshow/";
15 $this_url = "http://".$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];
16
17
18
19 if(isset($_REQUEST["showpic"])) {
20         // show the pic requested...
21         if(preg_match("/.*\/+.*/", $_REQUEST["showpic"])) {
22                 header("Status: 403 i dont think so");
23                 error_log("throwing 403");
24                 return;
25         }
26         
27         $afile = "$show_dir/".$_REQUEST["showpic"];
28         if(file_exists($afile)) {
29                 header('Content-Type: image/jpeg');
30                 echo file_get_contents($afile);
31         } else {
32                 header("Status: 404 Not Found");
33         }
34 } else if(isset($_REQUEST["fullpic"])) {
35         $file = $_REQUEST["fullpic"];
36         if(preg_match("/.*\/+.*/", $_REQUEST["fullpic"])) {
37                 error_log("throwing 403");
38                 header("Status: 403 i dont think so");
39                 return;
40         }
41         $afile = "$show_dir/".$_REQUEST["fullpic"];
42         if(file_exists($afile)) {
43                 echo "<html><body><h1>Fullsize picture $file</h1><a href=\"$this_url\"><img width=\"100%\" src=\"$this_url?showpic=$file\"></body></html>";
44         } else {
45                 header("Status: 403 i dont think so");
46                 return;
47         }
48         return;
49 } else if(isset($_REQUEST["medpic"])) {
50         $file = $_REQUEST["medpic"];
51         if(preg_match("/.*\/+.*/", $_REQUEST["medpic"])) {
52                 error_log("throwing 403");
53                 header("Status: 403 i dont think so");
54                 return;
55         }
56         $afile = "$show_dir/".$_REQUEST["medpic"];
57         if(file_exists($afile)) {
58                 echo "<html><body><h1>Picture $file</h1><a href=\"$this_url?fullpic=$file\"><img src=\"$this_url?showmedpic=$file\"></body></html>";
59         } else {
60                 header("Status: 403 i dont think so");
61                 return;
62         }
63         return;
64 } else if(isset($_REQUEST["showmedpic"])) {
65         if(preg_match("/.*\/+.*/", $_REQUEST["showmedpic"])) {
66                 error_log("throwing 403");
67                 header("Status: 403 i dont think so");
68                 return;
69         }
70         $afile = "$show_dir/".$_REQUEST["showmedpic"];
71         if(file_exists($afile)) {
72                 header('Content-Type: image/jpeg');
73                 $src = imagecreatefromjpeg($afile);
74                 list($width, $height) = getimagesize($afile);
75         
76                 if($width <= $med_size && $height <= $med_size) {
77                         // just pump out the pic
78                         error_log("pic pump");
79                         header('Content-Type: image/jpeg');
80                         echo file_get_contents($afile);
81                         return;
82                 }
83                 
84                 // else, resize it
85                 if($width > $height) {
86                         $nw = $med_size;
87                         $nh = (int)($height/($width/$med_size));
88                 } else {
89                         $nh = $med_size;
90                         $nw = (int)($width/($height/$med_size));
91                                 
92                 }
93         
94                 /*
95                  * 2000
96                 * 1800
97                 * $nw = 200
98                 * $mul = $height/$width/200
99                 */
100         
101                 // error_log("neww = $nw, $nh");
102                 $thumb = imagecreatetruecolor($nw, $nh);
103         
104                 imagecopyresized($thumb, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);
105                 imagejpeg($thumb);
106         } else {
107                 header("Status: 404 Not Found");
108         }
109 } else if(isset($_REQUEST["showtmp"])) {
110         // nothing yet
111         if(preg_match("/.*\/+.*/", $_REQUEST["showtmp"])) {
112                 error_log("throwing 403");
113                 header("Status: 403 i dont think so");
114                 return;
115         }
116         $afile = "$show_dir/".$_REQUEST["showtmp"];
117         if(file_exists($afile)) {
118                 header('Content-Type: image/jpeg');
119                 $src = imagecreatefromjpeg($afile);
120                 list($width, $height) = getimagesize($afile);
121                 
122                 if($width > $height) {
123                         $nw = $thumb_size;
124                         $nh = (int)($height/($width/$thumb_size));
125                 } else {
126                         $nh = $thumb_size;
127                         $nw = (int)($width/($height/$thumb_size));
128                         
129                 }
130                 
131                 /*
132                  * 2000
133                  * 1800
134                  * $nw = 200
135                  * $mul = $height/$width/200
136                  */
137                 
138                 // error_log("neww = $nw, $nh");
139                 $thumb = imagecreatetruecolor($nw, $nh);
140                 
141                 imagecopyresized($thumb, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);
142                 imagejpeg($thumb);
143         } else {
144                 header("Status: 404 Not Found");
145         }
146         return;
147 } else {
148         
149         // show a grid of images
150         ?>
151 <html>
152 <body>
153 <h1>Temp Show</h1>
154 <table>
155 <?php
156
157         // echo "<pre>";
158         // print_r($_SERVER);
159         // echo "</pre>";
160         $i = 0;
161
162         $dh = opendir($show_dir);
163         while (($file = readdir($dh)) !== false) {
164                 if(preg_match("/.*\.[jJ][pP][gG]$/", $file)) {
165                         if($i == 0) {
166                                 echo "<tr>";
167                         }
168                         
169                         echo "<td><a href=\"$this_url?medpic=$file\"><img src=\"$this_url?showtmp=$file\"></a></td>";
170                         
171                         if($i == ($n_per_line-1)) {
172                                 echo "</tr>";
173                                 $i = 0;
174                         } else {
175                                 $i++;
176                         }
177                 }
178         }
179                 
180
181 ?>
182 </table>
183 </body>
184 </html>
185 <?php
186         
187 }
188 ?>