Skip to content

Instantly share code, notes, and snippets.

@colinlord
Created October 7, 2020 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinlord/5ad14f185731a23bd3f4fbb0452631e4 to your computer and use it in GitHub Desktop.
Save colinlord/5ad14f185731a23bd3f4fbb0452631e4 to your computer and use it in GitHub Desktop.
Nest + Weather Data Overlay
<?php
/* GET OUR WEATHER DATA */
// Get our JSON
$strJsonFileContents = file_get_contents("YOUR API LINK GOES HERE");
$weather = json_decode($strJsonFileContents, true);
// Convert temperature and dew point
$temp = round($weather[currently][temperature]) . '°';
$dew = round($weather[currently][dewPoint]) . '°';
// Convert wind speed
$windNumber = round($weather[currently][windSpeed]);
$windSpeed = $windNumber . ' mph';
$windDirection = $weather[currently][windBearing];
$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;
/* CREATE OUR NEW IMAGE */
// Make the canvas
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(1920, 1080);
// Add overlay box
$camera = imagecreatefromjpeg('https://nexusapi-us1.camera.home.nest.com/get_image?uuid=e07868640e544a9d9d2cca77ea21894e&width=1920&public=ztIeg68tLB');
$overlay = imagecreatefrompng('time-overlay.png'); // full URL of overlay image is https://lordcol.in/roof-camera/time-overlay.png
imagecopy($canvas, $camera, 0, 0, 0, 0, 1920, 1080); // second zero is vertical orientation
imagecopy($canvas, $overlay, 0, 0, 0, 0, 1920, 1080);
// Put the weather and time in the overlay box
$color = imagecolorallocate($canvas, 255, 255, 255);
$date = date("m/d/y", time() - 3600);
$time = date("h:i A", time() - 3600);
$font = '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);
if ($windNumber > 0) {
imagettftext($canvas, 36, 0, 1050, 62, $white, $font, $wind);
} else {
imagettftext($canvas, 36, 0, 1050, 62, $white, $font, '0 mph');
}
// Save the image and free memory
imagejpeg($canvas, 'image-roof.jpg', 100);
imagedestroy($canvas);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment