-
Notifications
You must be signed in to change notification settings - Fork 1
/
webcam_screenshot.php
147 lines (130 loc) · 4.13 KB
/
webcam_screenshot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
include_once "lib/webcam_access.inc";
$_webcam = null;
$_webcam_width = -1;
$_webcam_height = -1;
$_webcam_size_ratio = 3/4;
$_timestamp = true;
$_webcam_name = "";
if (!array_key_exists("cam",$_GET))
{
die("Invalid Access");
}
switch ($_GET['cam'])
{
case "1":
$_webcam = CAMONE . "/image.jpg";
$_webcam_name = "Camera One";
break;
case "2":
$_webcam = CAMTWO . "/image.jpg";
$_webcam_name = "Camera Two";
break;
default:
die("Invalid Access");
break;
}
if (array_key_exists("width",$_GET))
{
$_webcam_width = $_GET["width"];
}
else
{
$_webcam_width = 640;
}
if (array_key_exists("stamp",$_GET))
{
if ($_GET["stamp"] == "0")
{
$_timestamp = false;
}
}
$_webcam_height = $_webcam_width * $_webcam_size_ratio;
$source = $_webcam . "?cache=" . getRandNumber();
doImage($source,$_webcam_width, $_webcam_height,$_timestamp,$_webcam_name);
function doImage($source,$width,$height,$timestamp=true,$name)
{
$origImage = getImage($source,$width,$height);
if (is_null($origImage)) { die("Rendering Error"); }
$image = resize($origImage,$width,$height);
$thumb = resize($origImage,100,75);
if ($timestamp)
{
$image = addTimeStamp($image);
}
$compression = 75;
$filepath = "screenshots/";
$filename = $filepath . $name . "_" . time() . ".jpg";
$filename_thumb = $filepath . $name . "_" . time() . "_thumb.jpg";
//main image
imagejpeg($image,$filename,$compression);
chmod($filename,0600);
imagedestroy($image);
//thumb image
imagejpeg($thumb,$filename_thumb,$compression);
chmod($filename_thumb,0600);
imagedestroy($thumb);
echo "OK";
}
function getImage($source,$width,$height)
{
$base_url = $source;
// The usual - init a curl session and set the url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
// Set your login and password for authentication
curl_setopt($ch, CURLOPT_USERPWD, USERIDPWD);
// You can use CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE,
// CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE
//
// You can use the bitwise | (or) operator to combine more than one method.
// If you do this, CURL will poll the server to see what methods it supports and pick the best one.
//
// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST |
// CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
//
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE |
// CURLAUTH_NTLM
//
// Personally I prefer CURLAUTH_ANY as it covers all bases
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// This is occassionally required to stop CURL from verifying the peer's certificate.
// CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if
// CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a
// common name and also verify that it matches the hostname provided)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Optional: Return the result instead of printing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// The usual - get the data and close the session
$data = curl_exec($ch);
curl_close($ch);
if (empty($data)) { die("Connection Error"); }
$image = imagecreatefromstring ($data);
return $image;
}
function addTimeStamp($image)
{
$text =date("n/j/y H:i:s", time());
$font = 2;
$w = imagefontwidth($font) * strlen($text) ;
$h = imagefontheight($font) ;
$x = imagesx($image) - $w ;
$y = imagesy($image) - $h;
//$backgroundColor = imagecolorallocate ($image, 255, 255, 255);
$fontcolor = imagecolorallocate ($image, 221,221,221);
imagestring ($image, $font, $x, $y, $text, $fontcolor);
return $image;
}
function resize($image,$width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
return $new_image;
}
function getRandNumber()
{
// $prefix = date("G").date("i").date("s").date("u");
$prefix = time();
$str = uniqid($prefix);
return $str;
}
?>