-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_functions.php
executable file
·161 lines (134 loc) · 5.09 KB
/
camera_functions.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
$settings = include('settings.php');
//$debug = include('debug.php');
$settings = $settings['mysql'];
$host = $settings['host'];
$db = $settings['dbname'];
$user = $settings['user'];
$pass = $settings['pass'];
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
function grab_snapshot($path=false,$make_thumb=false){
global $pdo;
$path = rtrim($path,'/');
$path = $path.'/';
$daylight = 0;
$username = 'admin';
$password = 'admin';
$url = "http://10.0.61.76/live/0/jpeg.jpg";
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$image = file_get_contents($url, false, $context);
if($image){
if(!$path){
return $image;
}
$t = time();
$filename = "{$path}{$t}.jpg";
$weathername = "{$path}{$t}.json";
file_put_contents($filename,$image);
$weather_json = json_decode(file_get_contents("http://api.openweathermap.org/data/2.5/weather?zip=76308&appid=7121f0abc97aacff5b6044b0970718e0"),true);
$words = [];
if(is_array($weather_json)){
foreach($weather_json['weather'] as $w){
$words[] = $w['main'];
}
$sunrise = $weather_json['sys']['sunrise'];
$sunset = $weather_json['sys']['sunset'];
$dt = $weather_json['dt'];
if($dt>$sunrise && $dt<$sunset){
$daylight = 1;
}
}
$wkeys = implode(',',$words);
//debug (print_r($wkeys,true), date('Y-m-d H:i:s').": weather keys ", "./logs/output.log");
file_put_contents($weathername,json_encode($weather_json)) ;
if($make_thumb){
$thumb_name = "{$path}{$t}_thumb.jpg";
make_thumb($filename, $thumb_name , 300);
$data = [1,0,$t,"","{$filename}","{$thumb_name}","",0,$wkeys,json_encode($weather_json),0,"",$daylight];
insert_image_data($data);
return ['filename'=>$filename,'thumbname'=>$thumb_name];
}else{
return $filename;
}
}else{
return false;
}
}
function insert_image_data($data){
global $pdo;
$sql = "SELECT MAX(image_id) + 1 FROM training_images;";
$statement = $pdo->prepare($sql);
$statement->execute();
$data[1] = $statement->fetchColumn();
$sql = "INSERT INTO `training_images` (`lot_id`, `image_id`, `date_created`, `date_last_saved`,`path`, `thumb_path`, `lot_data`, `classified`, `weather_keywords`, `weather_json`,`locked_time`,`edited_by`,`daylight`)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
//debug (print_r($data,true), date('Y-m-d H:i:s').": sql insert image ", "./logs/output.log");
$pdo->prepare($sql)->execute($data);
}
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
function grab_snapshot2($path='.',$save_image=false,$make_thumb=false){
$path = rtrim($path,'/');
$path = $path.'/';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://10.0.61.76/live/0/jpeg.jpg",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"authorization: Basic YWRtaW46YWRtaW4=",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: c1d37a0a-c220-f26f-ec09-0649ed6f4de2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$t = time();
$filename = "{$path}{$t}.jpg";
if(!$save_image){
return $response;
}
file_put_contents($filename,$response);
if($make_thumb){
$thumb_name = "{$path}{$t}_thumb.jpg";
make_thumb($filename, $thumb_name , 300);
return ['filename'=>$filename,'thumbname'=>$thumb_name];
}else{
return $filename;
}
}
}
?>