18
18
define ("_I_CACHE_EXPIRED " , "2592000 " ); // Expired time for images in seconds, 0 - never expired
19
19
20
20
21
- //$img=($_REQUEST['img']);
21
+ $ url = gr ('url ' );
22
+ $ img = gr ('img ' );
23
+ $ username = gr ('username ' );
24
+ $ password = gr ('password ' );
25
+ $ transport = gr ('transport ' );
22
26
23
- //$img=urldecode($_REQUEST['img']);
27
+ $ w = gr ('w ' , 'int ' );
28
+ $ h = gr ('h ' , 'int ' );
29
+ $ live = gr ('live ' , 'int ' );
24
30
25
- if (IsSet ($ url ) && $ url != '' ) {
31
+ $ debug_mode = gr ('debug ' , 'int ' );
32
+
33
+ if (isset ($ url ) && $ url != '' ) {
26
34
$ tmp_url = base64_decode ($ url );
27
35
if (!$ img ) {
28
36
$ filename = 'thumb_ ' . md5 ($ tmp_url ) . basename (preg_replace ('/\W/ ' , '' , $ tmp_url ));
36
44
$ resolution = $ w . 'x ' . $ h ;
37
45
}
38
46
//-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 ) {
41
49
echo $ cmd . '<br/> ' ;
42
50
} else {
43
51
exec ($ cmd );
48
56
}
49
57
}
50
58
51
- if ($ _GET [ ' debug ' ] ) {
59
+ if ($ debug_mode ) {
52
60
$ live = 0 ;
53
61
}
54
62
55
- if (IsSet ($ url ) && $ url != '' ) {
63
+ if (isset ($ url ) && $ url != '' ) {
56
64
57
65
$ resize = '' ;
58
66
if ($ w && $ h ) {
68
76
$ url = str_replace (':// ' , ':// ' . $ username . ': ' . $ password . '@ ' , $ url );
69
77
}
70
78
71
- if (preg_match ('/^rtsp:/is ' , $ url ) || preg_match ('/\/dev/ ' ,$ url )) {
79
+ if (preg_match ('/^rtsp:/is ' , $ url ) || preg_match ('/\/dev/ ' , $ url )) {
72
80
//-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) ;
76
84
}
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) ;
79
87
}
80
- $ cmd = PATH_TO_FFMPEG . ' ' . $ stream_options . ' ' . $ img ;
88
+ $ cmd = PATH_TO_FFMPEG . ' ' . $ stream_options . ' ' . escapeshellarg ( $ img) ;
81
89
82
90
if ($ live ) {
83
91
$ boundary = "my_mjpeg " ;
105
113
$ output = array ();
106
114
$ res = exec ($ cmd . ' 2>&1 ' , $ output );
107
115
108
- if ($ _GET [ ' debug ' ] ) {
116
+ if ($ debug_mode ) {
109
117
echo $ cmd ;
110
118
echo "<hr><pre> " . implode ("\n" , $ output ) . "</pre> " ;
111
119
exit ;
@@ -139,12 +147,11 @@ function mjpeg_grab_frame($url)
139
147
$ result = @mjpeg_grab_frame ($ url );
140
148
141
149
if (!$ result ) {
142
- $ url = preg_replace ('/\/\/(.+?)@/ ' ,'// ' ,$ url );
150
+ $ url = preg_replace ('/\/\/(.+?)@/ ' , '// ' , $ url );
143
151
$ result = getURL ($ url , 0 , $ username , $ password , false , array (CURLOPT_HTTPAUTH => CURLAUTH_ANY ));
144
152
}
145
153
146
154
147
-
148
155
if ($ result ) {
149
156
150
157
if ($ live ) {
@@ -163,7 +170,7 @@ function mjpeg_grab_frame($url)
163
170
for ($ i = 0 ; $ i < ob_get_level (); $ i ++) ob_end_flush ();
164
171
ob_implicit_flush (1 );
165
172
ob_end_flush ();
166
- $ counter= 0 ;
173
+ $ counter = 0 ;
167
174
print "Content-type: image/jpeg \n\n" ;
168
175
while (true ) {
169
176
$ counter ++;
@@ -190,23 +197,16 @@ function mjpeg_grab_frame($url)
190
197
}
191
198
192
199
193
- //$img=str_replace('\\\\', '\\', $img);
194
-
195
- //echo $img;exit;
196
-
197
- $ type = (IsSet ($ _REQUEST ['t ' ]) ? $ _REQUEST ['t ' ] : 0 );
200
+ $ type = gr ('t ' , 'int ' );
198
201
199
202
/*
200
203
Allowed types:
201
204
0 - fit
202
205
2 - exact size
203
206
*/
204
- //$img='./../../'.$img;
205
-
206
207
if (file_exists ($ img )) {
207
-
208
- $ new_width = (int )$ _REQUEST ['w ' ];
209
- $ new_height = (int )$ _REQUEST ['h ' ];
208
+ $ new_width = $ w ;
209
+ $ new_height = $ h ;
210
210
211
211
$ cached_filename = md5 ($ img . filemtime ($ img ) . $ new_width . $ new_height ) . '.jpg ' ;
212
212
$ path_to_cache_file = _I_CACHE_PATH . substr ($ cached_filename , 0 , 2 );
@@ -215,20 +215,14 @@ function mjpeg_grab_frame($url)
215
215
// Check the cache
216
216
if (_I_CACHING == "1 " && !$ dc )
217
217
218
- //$cache = _I_CACHE_PATH.md5($img.filemtime($img).$image_width.$image_height).".pic";
219
218
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
- // {
224
219
header ("Content-Type:image/jpeg " );
225
220
header ("Content-Length: " . filesize ($ cache ));
226
221
header ("Cache-Control: public " ); // HTTP/1.1
227
222
header ("Expires: " . gmdate ('D, d M Y H:i:s ' , (time () + 60 * 60 * 24 * 30 )) . ' GMT ' ); // Date in the future (+30 days)
228
223
header ('Last-Modified: ' . gmdate ('D, d M Y H:i:s ' , @filemtime ($ cache )) . ' GMT ' );
229
224
readfile ($ cache );
230
225
exit ;
231
- // }
232
226
}
233
227
234
228
$ filename = $ img ;
@@ -249,31 +243,14 @@ function mjpeg_grab_frame($url)
249
243
$ image_height = $ new_height ;
250
244
}
251
245
break ;
252
-
253
246
case 1 :
254
247
$ image_width = $ new_width ;
255
248
$ image_height = $ image_height ;
256
249
break ;
257
250
}
258
251
259
252
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
273
253
if ($ image_format == 1 ) {
274
- // Header("Content-Type:image/gif");
275
- // readfile($filename);
276
- // exit;
277
254
$ old_image = imagecreatefromgif ($ filename );
278
255
} elseif ($ image_format == 2 ) {
279
256
$ old_image = imagecreatefromjpeg ($ filename );
@@ -283,28 +260,19 @@ function mjpeg_grab_frame($url)
283
260
return ;
284
261
}
285
262
286
- // echo("$image_width x $image_height");
287
263
$ new_image = imageCreateTrueColor ($ image_width , $ image_height );
288
264
$ white = ImageColorAllocate ($ new_image , 255 , 255 , 255 );
289
265
ImageFill ($ new_image , 0 , 0 , $ white );
290
266
291
- /*imageCopyResized*/
292
267
imagecopyresampled ($ new_image , $ old_image , 0 , 0 , 0 , 0 , $ image_width , $ image_height , imageSX ($ old_image ), imageSY ($ old_image ));
293
268
294
269
// Save to cache
295
270
if (_I_CACHING == "1 " && !$ _REQUEST ['dc ' ]) {
296
- //if (!file_exists(_I_CACHE_PATH)) {
297
- // @mkdir(_I_CACHE_PATH, 0777);
298
- //}
299
271
if (!is_dir ($ path_to_cache_file )) {
300
272
@mkdir ($ path_to_cache_file );
301
273
}
302
-
303
274
imageJpeg ($ new_image , $ cache );
304
- //@chmod($cache, '0666');
305
275
}
306
- // Endof save
307
-
308
276
309
277
Header ("Content-type:image/jpeg " );
310
278
imageJpeg ($ new_image );
0 commit comments