2016-08-13 41 views
0

カメラからの画像が10秒ごとにアップロードされる2つのディレクトリから最新の画像を表示するには コードは機能しますが、わかっているように各ディレクトリには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
+0

新しい画像をアップロードすると、ファイル名をデータベースに保存するだけで、すべてのファイルを繰り返し処理するのではなく、ファイル名を取得して最新のものを取得します。 –

答えて

0

新しいファイルに名前が付いている場合はイースト/スナップ/ current.jpgサウス/スナップ/ current.jpg、あなたは簡単にこの

<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"> 
のようにもPHPずにHTMLを使用することができます

1 /スクリプトは、写真が既にアップロードさ電流をコピー

  • について責任を負わなければならないアップロードします。 jpg〜2016-08-13-07:00:00.jpg(または、日付
  • current.jpg

EDIT

私は上記の言ったようにあなたはファイル名を変更できない場合、これが得る

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

を試しに新しいファイルをコピーします現在の時間に撮影されたすべてのファイルを配列に格納します。その時間から画像が見つからないため、スクリプトが9秒間呼び出された場合、時間が変更された後にスクリプトが呼び出されると、問題が発生する可能性があります。しかし、この方法でスキャンしたファイルの数を最小限に抑えることができます。

EDIT

このスクリプトは、テストされていませんが、一つのフォルダのために1つの最近のファイルを返す必要があります:

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

回答を編集します。 –