1818define ("_I_CACHE_EXPIRED " , "2592000 " ); // Expired time for images in seconds, 0 - never expired
1919
2020
21- //$img=($_REQUEST['img']);
21+ $ url = gr ('url ' );
22+ $ img = gr ('img ' );
23+ $ username = gr ('username ' );
24+ $ password = gr ('password ' );
25+ $ transport = gr ('transport ' );
2226
23- //$img=urldecode($_REQUEST['img']);
27+ $ w = gr ('w ' , 'int ' );
28+ $ h = gr ('h ' , 'int ' );
29+ $ live = gr ('live ' , 'int ' );
2430
25- if (IsSet ($ url ) && $ url != '' ) {
31+ $ debug_mode = gr ('debug ' , 'int ' );
32+
33+ if (isset ($ url ) && $ url != '' ) {
2634 $ tmp_url = base64_decode ($ url );
2735 if (!$ img ) {
2836 $ filename = 'thumb_ ' . md5 ($ tmp_url ) . basename (preg_replace ('/\W/ ' , '' , $ tmp_url ));
3644 $ resolution = $ w . 'x ' . $ h ;
3745 }
3846 //-re -f v4l2 -video_size 1280x720 -i /dev/video0
39- $ cmd = 'fswebcam -r ' . $ resolution . ' ' . $ img_tmp ;
40- if ($ _GET [ ' debug ' ] ) {
47+ $ cmd = 'fswebcam -r ' . escapeshellarg ( $ resolution) . ' ' . escapeshellarg ( $ img_tmp) ;
48+ if ($ debug_mode ) {
4149 echo $ cmd . '<br/> ' ;
4250 } else {
4351 exec ($ cmd );
4856 }
4957}
5058
51- if ($ _GET [ ' debug ' ] ) {
59+ if ($ debug_mode ) {
5260 $ live = 0 ;
5361}
5462
55- if (IsSet ($ url ) && $ url != '' ) {
63+ if (isset ($ url ) && $ url != '' ) {
5664
5765 $ resize = '' ;
5866 if ($ w && $ h ) {
6876 $ url = str_replace (':// ' , ':// ' . $ username . ': ' . $ password . '@ ' , $ url );
6977 }
7078
71- if (preg_match ('/^rtsp:/is ' , $ url ) || preg_match ('/\/dev/ ' ,$ url )) {
79+ if (preg_match ('/^rtsp:/is ' , $ url ) || preg_match ('/\/dev/ ' , $ url )) {
7280 //-rtsp_transport tcp // -rtsp_transport tcp
73- $ stream_options = '-timelimit 15 -y -i " ' . $ url . ' " ' . $ resize . ' -r 5 -f image2 -vframes 1 ' ; //-ss 00:00:01.500
74- if ($ _GET [ ' debug ' ] ) {
75- $ stream_options = '-v verbose ' . $ stream_options ;
81+ $ stream_options = '-timelimit 15 -y -i ' . escapeshellarg ( $ url) . ( $ resize) . ' -r 5 -f image2 -vframes 1 ' ; //-ss 00:00:01.500
82+ if ($ debug_mode ) {
83+ $ stream_options = '-v verbose ' . escapeshellarg ( $ stream_options) ;
7684 }
77- if ($ _GET [ ' transport ' ] ) {
78- $ stream_options = '-rtsp_transport ' . $ _GET [ ' transport ' ] . ' ' . $ stream_options ;
85+ if ($ transport ) {
86+ $ stream_options = '-rtsp_transport ' . escapeshellarg ( $ transport) . ' ' . escapeshellarg ( $ stream_options) ;
7987 }
80- $ cmd = PATH_TO_FFMPEG . ' ' . $ stream_options . ' ' . $ img ;
88+ $ cmd = PATH_TO_FFMPEG . ' ' . $ stream_options . ' ' . escapeshellarg ( $ img) ;
8189
8290 if ($ live ) {
8391 $ boundary = "my_mjpeg " ;
105113 $ output = array ();
106114 $ res = exec ($ cmd . ' 2>&1 ' , $ output );
107115
108- if ($ _GET [ ' debug ' ] ) {
116+ if ($ debug_mode ) {
109117 echo $ cmd ;
110118 echo "<hr><pre> " . implode ("\n" , $ output ) . "</pre> " ;
111119 exit ;
@@ -139,12 +147,11 @@ function mjpeg_grab_frame($url)
139147 $ result = @mjpeg_grab_frame ($ url );
140148
141149 if (!$ result ) {
142- $ url = preg_replace ('/\/\/(.+?)@/ ' ,'// ' ,$ url );
150+ $ url = preg_replace ('/\/\/(.+?)@/ ' , '// ' , $ url );
143151 $ result = getURL ($ url , 0 , $ username , $ password , false , array (CURLOPT_HTTPAUTH => CURLAUTH_ANY ));
144152 }
145153
146154
147-
148155 if ($ result ) {
149156
150157 if ($ live ) {
@@ -163,7 +170,7 @@ function mjpeg_grab_frame($url)
163170 for ($ i = 0 ; $ i < ob_get_level (); $ i ++) ob_end_flush ();
164171 ob_implicit_flush (1 );
165172 ob_end_flush ();
166- $ counter= 0 ;
173+ $ counter = 0 ;
167174 print "Content-type: image/jpeg \n\n" ;
168175 while (true ) {
169176 $ counter ++;
@@ -190,23 +197,16 @@ function mjpeg_grab_frame($url)
190197}
191198
192199
193- //$img=str_replace('\\\\', '\\', $img);
194-
195- //echo $img;exit;
196-
197- $ type = (IsSet ($ _REQUEST ['t ' ]) ? $ _REQUEST ['t ' ] : 0 );
200+ $ type = gr ('t ' , 'int ' );
198201
199202/*
200203 Allowed types:
201204 0 - fit
202205 2 - exact size
203206*/
204- //$img='./../../'.$img;
205-
206207if (file_exists ($ img )) {
207-
208- $ new_width = (int )$ _REQUEST ['w ' ];
209- $ new_height = (int )$ _REQUEST ['h ' ];
208+ $ new_width = $ w ;
209+ $ new_height = $ h ;
210210
211211 $ cached_filename = md5 ($ img . filemtime ($ img ) . $ new_width . $ new_height ) . '.jpg ' ;
212212 $ path_to_cache_file = _I_CACHE_PATH . substr ($ cached_filename , 0 , 2 );
@@ -215,20 +215,14 @@ function mjpeg_grab_frame($url)
215215 // Check the cache
216216 if (_I_CACHING == "1 " && !$ dc )
217217
218- //$cache = _I_CACHE_PATH.md5($img.filemtime($img).$image_width.$image_height).".pic";
219218 if (file_exists ($ cache )) {
220- // $resc=GetImageSize($cache);
221- // list($w_o,$h_o,$t_o) = $resc;
222- // if ($w_o == $image_width && $h_o == $image_height && $t_o == $image_format)
223- // {
224219 header ("Content-Type:image/jpeg " );
225220 header ("Content-Length: " . filesize ($ cache ));
226221 header ("Cache-Control: public " ); // HTTP/1.1
227222 header ("Expires: " . gmdate ('D, d M Y H:i:s ' , (time () + 60 * 60 * 24 * 30 )) . ' GMT ' ); // Date in the future (+30 days)
228223 header ('Last-Modified: ' . gmdate ('D, d M Y H:i:s ' , @filemtime ($ cache )) . ' GMT ' );
229224 readfile ($ cache );
230225 exit ;
231- // }
232226 }
233227
234228 $ filename = $ img ;
@@ -249,31 +243,14 @@ function mjpeg_grab_frame($url)
249243 $ image_height = $ new_height ;
250244 }
251245 break ;
252-
253246 case 1 :
254247 $ image_width = $ new_width ;
255248 $ image_height = $ image_height ;
256249 break ;
257250 }
258251
259252
260- // Remove old cached images
261- /*
262- if ($handle = opendir(_I_CACHE_PATH)) {
263- while (false !== ($file = readdir($handle))) {
264- if ($file != "." && $file != ".." && time() - filemtime(_I_CACHE_PATH.$file) > _I_CACHE_EXPIRED)
265- @unlink(_I_CACHE_PATH.$file);
266- }
267- closedir($handle);
268- }
269- */
270- //
271-
272- // endof check
273253 if ($ image_format == 1 ) {
274- // Header("Content-Type:image/gif");
275- // readfile($filename);
276- // exit;
277254 $ old_image = imagecreatefromgif ($ filename );
278255 } elseif ($ image_format == 2 ) {
279256 $ old_image = imagecreatefromjpeg ($ filename );
@@ -283,28 +260,19 @@ function mjpeg_grab_frame($url)
283260 return ;
284261 }
285262
286- // echo("$image_width x $image_height");
287263 $ new_image = imageCreateTrueColor ($ image_width , $ image_height );
288264 $ white = ImageColorAllocate ($ new_image , 255 , 255 , 255 );
289265 ImageFill ($ new_image , 0 , 0 , $ white );
290266
291- /*imageCopyResized*/
292267 imagecopyresampled ($ new_image , $ old_image , 0 , 0 , 0 , 0 , $ image_width , $ image_height , imageSX ($ old_image ), imageSY ($ old_image ));
293268
294269 // Save to cache
295270 if (_I_CACHING == "1 " && !$ _REQUEST ['dc ' ]) {
296- //if (!file_exists(_I_CACHE_PATH)) {
297- // @mkdir(_I_CACHE_PATH, 0777);
298- //}
299271 if (!is_dir ($ path_to_cache_file )) {
300272 @mkdir ($ path_to_cache_file );
301273 }
302-
303274 imageJpeg ($ new_image , $ cache );
304- //@chmod($cache, '0666');
305275 }
306- // Endof save
307-
308276
309277 Header ("Content-type:image/jpeg " );
310278 imageJpeg ($ new_image );
0 commit comments