JavaScript、PHP 网络摄像头图像的数组帮助,
JavaScript, PHP Array help for Webcam Images,
我真的不想post但我已经束手无策了。
我有一个相机,可以用来向访客显示当前的天气状况。
我有一个 powershell 脚本保存来自相机的快照并通过 PSFTP 将它们上传到我的 ftp。文件名如下 'MMddyyyyhhmmss'HBGCWeatherCam.jpeg.
这些文件位于 /images 文件夹中
要求:
我需要读取图像文件夹中的所有文件,按日期降序排序,然后将其传递给 'var images = [], x = -1;'
调用的 javascript 数组
但我似乎无法让它正常工作。
如有任何帮助,我们将不胜感激。
编辑——下面是完整的工作代码,非常感谢Digital_Chris
<html>
<head>
<title>Weather Cam</title>
<br/>
<br/>
<script type = "text/javascript">
function displayPreviousImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayNextImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
//function startTimer() {
// setInterval(displayNextImage, 500);
//clearInterval(startTimer);
//}
var images = [], x = -1;
<?php
$my_files = glob("images/*.jpeg");
$counter = 0;
foreach(array_reverse(glob("images/*.jpeg")) as $file) {
echo "images[" . $counter . "] = '" . $file . "'; ";
$counter++;
}
?>
</script>
</head>
<body onload = "displayPreviousImage()">
<div align="center">
<img id="img" src="images/loading.gif" style="width: 500px; height: 500px;"/>
<br/>
<button style="width: 100;" onclick="displayPreviousImage()">< Previous</button>
View Other Snapshots
<button style="width: 100;" onclick="displayNextImage()">Next ></button>
<br/>
<button style="width: 100;" onClick="window.location.reload()">View Current</button>
</div>
</body>
</html>
此答案假设您已成功将文件名数组放入 $my_files
:
在您的 javascript 块中,您需要对图像进行 php 循环:
var images = [], x = -1;
<?php
$counter = 0;
foreach($my_files as $file) {
echo "images[" . $counter . "] = 'images/" . $file . "'; ";
$counter++;
}
?>
</script>
这将产生如下输出:
images[0] = 'images/somefile.jpeg';
images[1] = 'images/anotherfile.jpeg';
images[2] = 'images/whateversInTheDir.jpeg';
</script>
我真的不想post但我已经束手无策了。
我有一个相机,可以用来向访客显示当前的天气状况。
我有一个 powershell 脚本保存来自相机的快照并通过 PSFTP 将它们上传到我的 ftp。文件名如下 'MMddyyyyhhmmss'HBGCWeatherCam.jpeg.
这些文件位于 /images 文件夹中
要求: 我需要读取图像文件夹中的所有文件,按日期降序排序,然后将其传递给 'var images = [], x = -1;'
调用的 javascript 数组但我似乎无法让它正常工作。
如有任何帮助,我们将不胜感激。
编辑——下面是完整的工作代码,非常感谢Digital_Chris
<html>
<head>
<title>Weather Cam</title>
<br/>
<br/>
<script type = "text/javascript">
function displayPreviousImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayNextImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
//function startTimer() {
// setInterval(displayNextImage, 500);
//clearInterval(startTimer);
//}
var images = [], x = -1;
<?php
$my_files = glob("images/*.jpeg");
$counter = 0;
foreach(array_reverse(glob("images/*.jpeg")) as $file) {
echo "images[" . $counter . "] = '" . $file . "'; ";
$counter++;
}
?>
</script>
</head>
<body onload = "displayPreviousImage()">
<div align="center">
<img id="img" src="images/loading.gif" style="width: 500px; height: 500px;"/>
<br/>
<button style="width: 100;" onclick="displayPreviousImage()">< Previous</button>
View Other Snapshots
<button style="width: 100;" onclick="displayNextImage()">Next ></button>
<br/>
<button style="width: 100;" onClick="window.location.reload()">View Current</button>
</div>
</body>
</html>
此答案假设您已成功将文件名数组放入 $my_files
:
在您的 javascript 块中,您需要对图像进行 php 循环:
var images = [], x = -1;
<?php
$counter = 0;
foreach($my_files as $file) {
echo "images[" . $counter . "] = 'images/" . $file . "'; ";
$counter++;
}
?>
</script>
这将产生如下输出:
images[0] = 'images/somefile.jpeg';
images[1] = 'images/anotherfile.jpeg';
images[2] = 'images/whateversInTheDir.jpeg';
</script>