-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.php
executable file
·70 lines (57 loc) · 2.55 KB
/
layer.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
<?php
/* GET OUR WEATHER DATA */
// Get our environment variables
include('.env.php');
// Get our JSON
// Documentation: https://ibm.co/v2PWSCC
$data = file_get_contents("https://api.weather.com/v2/pws/observations/current?stationId=" . $_ENV['STATION_ID'] . "&format=json&units=e&apiKey=" . $_ENV['API_KEY']);
$weather = json_decode($data, true);
if(is_string($weather['observations'][0]['stationID'])) {
// Successfully obtained our weather station data
// Convert temperature and dew point
$temp = $weather['observations'][0]['imperial']['temp'] . '°';
$dew = $weather['observations'][0]['imperial']['dewpt'] . '°';
// Convert wind speed
$windNumber = $weather['observations'][0]['imperial']['windSpeed'];
$windDirection = $weather['observations'][0]['winddir'];
if ($windDirection) {
$windSpeed = $windNumber . ' mph';
$compass = array('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW');
$windDirectionName = $compass[round( ($windDirection % 360 - 11.25) / 22.5)];
$wind = $windSpeed.' '.$windDirectionName;
} else {
$wind = 'Calm';
}
} else {
// We weren't able to obtain our weather data so report nothing
$temp = '';
$dew = '';
$wind = '';
}
/* CREATE OUR NEW IMAGE */
// Make the canvas
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(1920, 1080);
// Add overlay box
$offline = imagecreatefromjpeg(__DIR__.'/camera-offline.jpg');
$camera = imagecreatefromjpeg($_ENV['CAMERA']);
$overlay = imagecreatefrompng(__DIR__.'/time-overlay.png');
imagecopy($canvas, $offline, 0, 0, 0, 0, 1920, 1080);
imagecopy($canvas, $camera, 0, 0, 0, 0, 1920, 1080);
imagecopy($canvas, $overlay, 0, 0, 0, 0, 1920, 1080); // second zero is vertical orientation
// Put the weather and time in the overlay box
$color = imagecolorallocate($canvas, 255, 255, 255);
$date = date("m/d/y", time());
$time = date("h:i A", time());
putenv('GDFONTPATH=' . realpath('.'));
$font = __DIR__.'/sofia.ttf';
$white = imagecolorallocate(imagecreatetruecolor(1920, 1080), 255, 255, 255);
imagettftext($canvas, 36, 0, 1405, 62, $white, $font, $date);
imagettftext($canvas, 36, 0, 1680, 62, $white, $font, $time);
imagettftext($canvas, 36, 0, 390, 62, $white, $font, $temp);
imagettftext($canvas, 36, 0, 780, 62, $white, $font, $dew);
imagettftext($canvas, 36, 0, 1050, 62, $white, $font, $wind);
// Save the image and free memory
imagejpeg($canvas, __DIR__.'/camera.jpg', 100);
imagedestroy($canvas);
?>