2016-08-13 131 views
0

我使用它來顯示來自兩個目錄的最新圖像,其中來自攝像機的圖像每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> 
+0

如何獲得目錄填充文件?一些腳本這樣做?或者只是手動? –

+1

擺脫' chris85

+0

當您上傳新圖像時,只需將其文件名保存到數據庫以檢索其文件名,而不是遍歷所有文件並獲取最新的文件。 –

回答

0

如果新文件被命名爲東/快/ current.jpg南/卡/ current.jpg,你可以很容易地使用HTML,即使沒有PHP這樣

<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"> 

一個/腳本上傳的照片應該是

  • 複製已經上傳的當前。 jpg2016-08-13-07:00:00.jpg(或其他一些包含eg。日期
  • 複製新文件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"> 
+0

嗨,感謝您的幫助。 – Greg

+0

攝像頭通過ftp上傳到目錄。上傳時,我無法通過相機更改文件名。文件名格式爲:Schedule_20160812-123433.jpg – Greg

+0

我將編輯我的答案。 –