fixing webcam issues

This commit is contained in:
Janis Steiner
2024-10-14 12:38:32 +01:00
parent 5e853f4000
commit 3e1380ca47
2 changed files with 42 additions and 15 deletions

View File

@@ -1,26 +1,41 @@
<!DOCTYPE html>
<html>
<?php
$username=htmlspecialchars($_GET["username"]);
$printer_url=$_GET["url"];
$rotation=$_GET["rotation"];
$username = htmlspecialchars($_GET["username"]);
$printer_url = $_GET["url"];
$rotation = $_GET["rotation"];
?>
<head>
<title>Webcam</title>
<title>Webcam</title>
</head>
<body>
<?php
$path = "/var/www/html/user_files/$username/$printer_url.jpeg";
unlink($path);
exec("wget --quiet \"http://$printer_url/webcam/?action=snapshot\" -O $path");
echo("<img style='transform: rotate(".$rotation."deg);' loading='lazy' width='100%' src='/user_files/$username/$printer_url.jpeg'>");
?>
<!-- Display the first image -->
<img id="webcam-image" style="transform: rotate(<?php echo $rotation; ?>deg);" width="100%" src="/user_files/<?php echo $username; ?>/<?php echo $printer_url; ?>.jpeg" alt="Webcam Feed">
<script>
setInterval(function() {
location.reload();
}, 5000);
// Function to call PHP script to download the latest image and then swap it
function loadAndSwapImage() {
// Make an AJAX call to PHP to trigger the image download
let xhr = new XMLHttpRequest();
xhr.open('GET', '/api/download_image.php?username=<?php echo $username; ?>&url=<?php echo $printer_url; ?>', true);
xhr.onload = function() {
if (xhr.status === 200) {
// Preload the new image once the PHP script has completed downloading
let img = new Image();
img.src = '/user_files/<?php echo $username; ?>/<?php echo $printer_url; ?>.jpeg?rand=' + Math.random(); // Add cache buster to avoid caching issues
// Swap the image when it is loaded
img.onload = function() {
let webcamImage = document.getElementById('webcam-image');
webcamImage.src = img.src;
}
}
};
xhr.send(); // Execute the request
}
// Reload the image every 5 seconds
setInterval(loadAndSwapImage, 5000);
</script>
</body>
</html>