显示目录中的最新图像


Display latest image from directory

我使用它来显示来自两个目录的最新图像,其中来自相机的图像每隔10秒上传一次代码可以工作,但是正如我所知,每个目录中可能会有成千上万的图像,我认为代码没有针对这种情况进行优化。此外,我每隔10秒重新加载整个页面,也许更新图像会更有效。有人能给我指路优化这个吗?非常感谢。

<?php
    $page = $_SERVER['PHP_SELF'];
    $sec = "10";
    $base_url_east = 'East/snap/';
    $base_url_south = 'South/snap/';
    $newest_mtime_east = 0;
    $show_file_east = 'BROKEN';
    if ($handle = opendir($base_url_east)) {
        while (false !== ($file = readdir($handle))) {
            if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
                $mtime = filemtime("$base_url_east/$file");
                if ($mtime > $newest_mtime_east) {
                    $newest_mtime_east = $mtime;
                    $show_file_east = "$base_url_east/$file";
                }
            }
        }
    }
    $newest_mtime_south = 0;
    $show_file_south = 'BROKEN';
    if ($handle = opendir($base_url_south)) {
        while (false !== ($file = readdir($handle))) {
            if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
                $mtime = filemtime("$base_url_south/$file");
                if ($mtime > $newest_mtime_south) {
                    $newest_mtime_south = $mtime;
                    $show_file_south = "$base_url_south/$file";
                }
            }
        }
    }
?>
<html>
    <head>
        <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
    </head>
    <body bgcolor="#000000">
        <center>
        <?php
            print '<img src="' .$show_file_east. '" alt="Latest image uploaded" width="720" height="480">';
            print '<img src="' .$show_file_south. '" alt="Latest image uploaded" width="720" height="480">';
        ?>
        </center>
    </body>
</html>

如果新文件命名为East/snap/current.jpgSouth/snap/current.jpg,即使没有PHP,也可以像这样轻松地使用HTML

<img src="East/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">
<img src="South/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">

上传照片的人应该负责

  • 将已经上传的current.jpg复制到2016-08-13: 07:00:00.jpg(或其他包含eg. s .jpg的文件名)。
  • 将新文件复制到current.jpg

编辑

如果你不能像我上面说的那样修改文件名,试试

$string = date('Ymd-H'); // 20160812-12
$files = glob('Schedule_' . $string . '*.jpg');

这将把当前小时内拍摄的所有文件放入数组中。如果脚本在小时更改后的第9秒被调用,可能会遇到问题,因为它找不到那个小时的图像。但是用这种方法你可以最小化扫描文件的数量。

编辑

这个脚本没有被测试,但是应该返回一个文件夹的一个最近的文件:

<?php
$basePath = '/home/user/camera/East/snap';
// Scans files from the current and the last minutes
// this ensures that always files will be found, even if the minute changed
$date = new 'DateTime();
$date->setTimeZone(new 'DateTimeZone('America/Vancouver'));
$prefixCurrentMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$date->sub(new 'DateInterval('PT1M'));
$prefixLastMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$files = array_merge(
    glob($prefixLastMinute),
    glob($prefixCurrentMinute)
);
$lastFile = 'dummy.jpg';
if (is_array($files) AND count($files) > 0) {
    // this methods sorts the files "regularly" by default, and I guess
    // regularly means alpha-numeric in ascending order ...
    sort($files);
    $lastFile = $files[0];
    // use this, if the order is not ascending after using sort() on the array
    // $lastFile = $files[count($files) - 1];
}
$eastPhoto = basename($lastFile);
?>
<img src="East/snap/<?php echo $eastPhoto; ?>" alt="Latest image uploaded" width="720" height="480">