Skip to content

Instantly share code, notes, and snippets.

@mauron85
Last active October 21, 2019 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauron85/d8dfe5fe627b9ed5b1180cf1fed6e54b to your computer and use it in GitHub Desktop.
Save mauron85/d8dfe5fe627b9ed5b1180cf1fed6e54b to your computer and use it in GitHub Desktop.
<?php
$now = time();
$currentYear = date("Y", $now);
$currentMonth = date("m", $now);
$currentDay = date("d", $now);
$currentHour = date('G', $now);
$currentMinute = intval(date('i', $now));
$dir = realpath(dirname(__DIR__))
. DIRECTORY_SEPARATOR . 'cam'
. DIRECTORY_SEPARATOR . $currentYear
. DIRECTORY_SEPARATOR . $currentMonth
. DIRECTORY_SEPARATOR . $currentDay;
shell_exec('LD_LIBRARY_PATH=$LD_LIBRARY_PATH:' . realpath(__DIR__) . ' '
. realpath(__DIR__) . DIRECTORY_SEPARATOR . 'gepm.log -y -r 10 -pattern_type glob -i "'
. $dir . DIRECTORY_SEPARATOR . '*.jpg" -s hd720 -vcodec libvpx -qmin 0 -qmax 50 -crf 3 -b:v 1M -an '
. $dir . DIRECTORY_SEPARATOR. 'timelapse.webm </dev/null >/dev/null 2>timelapse.log &');
//print_r($output);
<?php
define('SNAPSHOT_DIR', 'cam');
define('FILE_PATTERN', '/timelapse.webm/');
define('DAY_IN_SECONDS', (3600 * 24));
define('NOT_AVAILABLE_MSG', 'Timelapse not available');
$relDay = is_numeric($_GET['rel_day']) ? intval($_GET['rel_day']) : 0;
$tonight = strtotime('today midnight');
$timestamp = $tonight + (DAY_IN_SECONDS * $relDay);
$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
$dir = SNAPSHOT_DIR
. DIRECTORY_SEPARATOR . $year
. DIRECTORY_SEPARATOR . $month
. DIRECTORY_SEPARATOR . $day;
$realDir = realpath(__DIR__) . DIRECTORY_SEPARATOR . $dir;
// get array of files within $dir, alphabetically sorted descending (newest first)
$files = @scandir($realDir, 1);
if ($files === false) {
header('HTTP/1.0 404 Not Found');
show_placeholder(NOT_AVAILABLE_MSG);
exit(1);
}
// get most recent uploaded file
$file = get_latest_file($files);
if ($file === false) {
header('HTTP/1.0 404 Not Found');
show_placeholder(NOT_AVAILABLE_MSG);
exit(1);
}
$filePath = $realDir . DIRECTORY_SEPARATOR . $file;
$fileTimestamp = filemtime($filePath);
$expire = gmdate('D, d M Y H:i:s', ($tonight + DAY_IN_SECONDS)) . ' GMT';
header('Expires: ' . $expire);
header('Pragma: cache');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
<head>
<title>Penzión Hello - Vysoké Tatry, ubytovanie, timelapse</title>
<style>
body,html {
margin: 0;
padding: 0;
background: #000;
}
</style>
</head>
<body>
<?php
echo '<video src="' . $dir . DIRECTORY_SEPARATOR . $file . '" with="720" height="405" controls autoplay />';
function get_latest_file($files) {
foreach ($files as $file) {
if (preg_match(FILE_PATTERN, $file, $matches) === 1) {
return $file;
}
}
return false;
}
function show_placeholder($text) {
/**
* Dynamic Dummy Image Generator — as seen on DummyImage.com
*
* This script enables you to create placeholder images in a breeze.
* Please refer to the README on how to use it.
*
* (Original idea by Russel Heimlich. When I first published this script,
* DummyImage.com was not Open Source, so I had to write a small script to
* replace the function on my own server.)
*
* @author Fabian Beiner <fb@fabianbeiner.de>
* @license MIT
* @link https://github.com/FabianBeiner/PHP-Dummy-Image-Generator/
* @version 0.3.0 <2017-12-26>
*/
/**
* Handle the “size” parameter.
*/
$size = '640x480';
list($imgWidth, $imgHeight) = explode('x', $size . 'x');
if ($imgHeight === '') {
$imgHeight = $imgWidth;
}
$filterOptions = array(
'options' => array(
'min_range' => 0,
'max_range' => 9999
)
);
if (filter_var($imgWidth, FILTER_VALIDATE_INT, $filterOptions) === false) {
$imgWidth = '640';
}
if (filter_var($imgHeight, FILTER_VALIDATE_INT, $filterOptions) === false) {
$imgHeight = '480';
}
/**
* Handle the “type” parameter.
*/
$type = 'png';
/**
* Handle the “text” parameter.
*/
$encoding = mb_detect_encoding($text, 'UTF-8, ISO-8859-1');
if ($encoding !== 'UTF-8') {
$text = mb_convert_encoding($text, 'UTF-8', $encoding);
}
$text = mb_encode_numericentity($text,
array(0x0, 0xffff, 0, 0xffff),
'UTF-8');
/**
* Handle the “bg” parameter.
*/
$bg = '000000';
list($bgRed, $bgGreen, $bgBlue) = sscanf($bg, '%02x%02x%02x');
/**
* Handle the “color” parameter.
*/
$color = 'FFFFFF';
list($colorRed, $colorGreen, $colorBlue) = sscanf($color, '%02x%02x%02x');
/**
* Define the typeface settings.
*/
$fontFile = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'RobotoMono-Regular.ttf';
if ( ! is_readable($fontFile)) {
$fontFile = 'arial';
}
$fontSize = round(($imgWidth - 50) / 8);
if ($fontSize <= 9) {
$fontSize = 9;
}
/**
* Generate the image.
*/
$image = imagecreatetruecolor($imgWidth, $imgHeight);
$colorFill = imagecolorallocate($image, $colorRed, $colorGreen, $colorBlue);
$bgFill = imagecolorallocate($image, $bgRed, $bgGreen, $bgBlue);
imagefill($image, 0, 0, $bgFill);
$textBox = imagettfbbox($fontSize, 0, $fontFile, $text);
while ($textBox[4] >= $imgWidth) {
$fontSize -= round($fontSize / 2);
$textBox = imagettfbbox($fontSize, 0, $fontFile, $text);
if ($fontSize <= 9) {
$fontSize = 9;
break;
}
}
$textWidth = abs($textBox[4] - $textBox[0]);
$textHeight = abs($textBox[5] - $textBox[1]);
$textX = ($imgWidth - $textWidth) / 2;
$textY = ($imgHeight + $textHeight) / 2;
imagettftext($image, $fontSize, 0, $textX, $textY, $colorFill, $fontFile, $text);
/**
* Return the image and destroy it afterwards.
*/
switch ($type) {
case 'png':
header('Content-Type: image/png');
imagepng($image, null, 9);
break;
case 'gif':
header('Content-Type: image/gif');
imagegif($image);
break;
case 'jpg':
case 'jpeg':
header('Content-Type: image/jpeg');
imagejpeg($image);
break;
}
imagedestroy($image);
}
?>
<script src="//www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">
_uacct = "UA-199075-1";
urchinTracker();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment