master, master where's the dreams that i've been after?
[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["medpic"])) {
35         $file = $_REQUEST["medpic"];
36         if(preg_match("/.*\/+.*/", $_REQUEST["medpic"])) {
37                 error_log("throwing 403");
38                 header("Status: 403 i dont think so");
39                 return;
40         }
41         $afile = "$show_dir/".$_REQUEST["medpic"];
42         if(file_exists($afile)) {
43                 echo "<html><body><h1>Picture $file</h1><a href=\"$this_url?showpic=$file\"><img src=\"$this_url?showmedpic=$file\"></body></html>";
44         } else {
45                 header("Status: 403 i dont think so");
46                 return;
47         }
48 } else if(isset($_REQUEST["showmedpic"])) {
49         if(preg_match("/.*\/+.*/", $_REQUEST["showmedpic"])) {
50                 error_log("throwing 403");
51                 header("Status: 403 i dont think so");
52                 return;
53         }
54         $afile = "$show_dir/".$_REQUEST["showmedpic"];
55         if(file_exists($afile)) {
56                 header('Content-Type: image/jpeg');
57                 $src = imagecreatefromjpeg($afile);
58                 list($width, $height) = getimagesize($afile);
59         
60                 if($width <= $med_size && $height <= $med_size) {
61                         // just pump out the pic
62                         error_log("pic pump");
63                         header('Content-Type: image/jpeg');
64                         echo file_get_contents($afile);
65                         return;
66                 }
67                 
68                 // else, resize it
69                 if($width > $height) {
70                         $nw = $med_size;
71                         $nh = (int)($height/($width/$med_size));
72                 } else {
73                         $nh = $med_size;
74                         $nw = (int)($width/($height/$med_size));
75                                 
76                 }
77         
78                 /*
79                  * 2000
80                 * 1800
81                 * $nw = 200
82                 * $mul = $height/$width/200
83                 */
84         
85                 // error_log("neww = $nw, $nh");
86                 $thumb = imagecreatetruecolor($nw, $nh);
87         
88                 imagecopyresized($thumb, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);
89                 imagejpeg($thumb);
90         } else {
91                 header("Status: 404 Not Found");
92         }
93 } else if(isset($_REQUEST["showtmp"])) {
94         // nothing yet
95         if(preg_match("/.*\/+.*/", $_REQUEST["showtmp"])) {
96                 error_log("throwing 403");
97                 header("Status: 403 i dont think so");
98                 return;
99         }
100         $afile = "$show_dir/".$_REQUEST["showtmp"];
101         if(file_exists($afile)) {
102                 header('Content-Type: image/jpeg');
103                 $src = imagecreatefromjpeg($afile);
104                 list($width, $height) = getimagesize($afile);
105                 
106                 if($width > $height) {
107                         $nw = $thumb_size;
108                         $nh = (int)($height/($width/$thumb_size));
109                 } else {
110                         $nh = $thumb_size;
111                         $nw = (int)($width/($height/$thumb_size));
112                         
113                 }
114                 
115                 /*
116                  * 2000
117                  * 1800
118                  * $nw = 200
119                  * $mul = $height/$width/200
120                  */
121                 
122                 // error_log("neww = $nw, $nh");
123                 $thumb = imagecreatetruecolor($nw, $nh);
124                 
125                 imagecopyresized($thumb, $src, 0, 0, 0, 0, $nw, $nh, $width, $height);
126                 imagejpeg($thumb);
127         } else {
128                 header("Status: 404 Not Found");
129         }
130         return;
131 } else {
132         
133         // show a grid of images
134         ?>
135 <html>
136 <body>
137 <h1>Temp Show</h1>
138 <table>
139 <?php
140
141         // echo "<pre>";
142         // print_r($_SERVER);
143         // echo "</pre>";
144         $i = 0;
145
146         $dh = opendir($show_dir);
147         while (($file = readdir($dh)) !== false) {
148                 if(preg_match("/.*\.[jJ][pP][gG]$/", $file)) {
149                         if($i == 0) {
150                                 echo "<tr>";
151                         }
152                         
153                         echo "<td><a href=\"$this_url?medpic=$file\"><img src=\"$this_url?showtmp=$file\"></a></td>";
154                         
155                         if($i == ($n_per_line-1)) {
156                                 echo "</tr>";
157                                 $i = 0;
158                         } else {
159                                 $i++;
160                         }
161                 }
162         }
163                 
164
165 ?>
166 </table>
167 </body>
168 </html>
169 <?php
170         
171 }
172 ?>