2011-12-20 54 views
0

我試圖建立一個網站,從服務器上的文件夾拉4張隨機照片,並提出他們在一個div中的一個小模塊。發生了什麼是相同的4張照片被稱爲。我怎樣才能打電話給4張不同的照片?php腳本顯示從一個文件夾中的4個隨機照片顯示4次相同的照片

繼承人的random.php文件:

<?php 
$folder = '.'; 

$extList = array(); 
$extList['jpg'] = 'image/jpeg'; 
$extList['jpeg'] = 'image/jpeg'; 

$img = null; 

if (substr($folder,-1) != '/') { 
$folder = $folder.'/'; 
} 

if (isset($_GET['img'])) { 
$imageInfo = pathinfo($_GET['img']); 
if (
    isset($extList[ strtolower($imageInfo['extension']) ]) && 
    file_exists($folder.$imageInfo['basename']) 
) { 
    $img = $folder.$imageInfo['basename']; 
} 
} else { 
$fileList = array(); 
$handle = opendir($folder); 
while (false !== ($file = readdir($handle))) { 
    $file_info = pathinfo($file); 
    if (
     isset($extList[ strtolower($file_info['extension']) ]) 
    ) { 
     $fileList[] = $file; 
    } 
} 
closedir($handle); 

if (count($fileList) > 0) { 
    $imageNumber = time() % count($fileList); 
    $img = $folder.$fileList[$imageNumber]; 
} 
} 

if ($img!=null) { 
$imageInfo = pathinfo($img); 
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ]; 
header ($contentType); 
readfile($img); 
} else { 
if (function_exists('imagecreate')) { 
    header ("Content-type: image/png"); 
    $im = @imagecreate (100, 100) 
     or die ("Can't initialize image stream"); 
    $background_color = imagecolorallocate ($im, 255, 255, 255); 
    $text_color = imagecolorallocate ($im, 0,0,0); 
    imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color); 
    imagepng ($im); 
    imagedestroy($im); 
} 
} 

?> 

這裏的random.php是如何被稱爲:

<?php 

/* DATA */ 
$data = array(
array('COUNT', '88', 'Here are a few of them', '1, 2, 3, 4'), 
); 
?> 

<div> 
<table class="reop" border='0' width='100%' cellpadding='0' cellspacing='10'> 

<?php 
$count = 0; 
foreach($data as $row) { 
    $class = ($count % 2 == 1 ? " class='alt'" : ''); 
    echo "<tr$class>"; 
    for($j = 0; $j < count($row); $j++) { 
     if ($j!=3) { 
      echo "<td class='cell_$j'>$row[$j]</td>"; 
     } else { 


//   $avatar = ''; 
      $array = preg_split('/,/', $row[$j], -1, PREG_SPLIT_NO_EMPTY); 
      foreach ($array as $val) { 
      $avatar .= '<img src="/staffpics/random.php"> '; 
      } 
      echo "<td class='cell_$j'>$avatar</td>"; 

     } 

    } 
    echo '</tr>'; 
// $count++; 
} 

?> 

</table> 

+0

我沒有答案,但我確實知道爲什麼4張圖片會出現相同情況:您的瀏覽器會緩存所有圖片。一旦它從服務器加載了一個圖像,就認爲不需要再次加載相同的圖像(即具有相同的文件名)。那麼也許給他們4個不同的名字? – 2011-12-20 19:38:29

+0

@Mr李斯特 - 這聽起來像是對我的回答。 – 2011-12-20 19:39:38

+0

@MrLister - 嗯,/ staffpics /中的所有圖像都命名爲[1.jpg,2.jpg。 3.jpg,...]這是你的意思嗎? – tronjavolta 2011-12-20 19:41:25

回答

2

當你的頁面被顯示時,大多數瀏覽器會立即請求四張圖片......幾乎所有的同時。你的「隨機」選擇圖片的代碼是基於time(),它返回自unixtime開始以來的秒數。在相同的秒內執行4次它也會產生4次相同的結果。

代碼:

$imageNumber = time() % count($fileList); 
$img = $folder.$fileList[$imageNumber]; 

應該寫成:

// filelist starts with "0", count with "1" .. so we are off-by-one and first picture would never show without -1 
$imageNumber = rand() % (count($fileList) - 1); 
$img = $folder.$fileList[$imageNumber]; 

這樣你會得到 「隨機」 的照片。

另外,李斯特先生評論的是完全有效的 - 您的瀏覽器也可能緩存圖片,因此只請求一次圖片,然後使用緩存一次又一次顯示該圖片。

避免這種情況,更換任何​​與<img src="random.php?rand=<?= rand(); ?>">


但請注意:

您的服務器可能最終相同的隨機數多次返回所以最終(很少),你將最終獲得相同圖片兩次在同一頁上。

要解決這個問題,您必須完全重寫您的代碼。

渲染頁面的腳本應該決定選擇哪些圖片,並確保沒有圖片顯示兩次,而不是隻包含一個「渲染隨機圖片」腳本,它與呈現的頁面無關。

容易得多,你可以試試這個:

// find all images - case SenSItivE 
$all_images = glob("/path/to/images/*.{jpeg|jpg|png|gif}", GLOB_BRACE); 
// bring array in random order 
shuffle($all_images); 
// pick four random images - you may also use a for or foreach loop to iterate the array 
list ($img1, $img2, $img3, $img4) = $all_images; 
// write code to display four images here 
+0

對@Mr Lister – Kaii 2011-12-20 20:48:30

+0

添加了解釋糟糕,我甚至沒有看到OP在他的代碼中沒有rand()! – 2011-12-21 13:20:48

1

這裏是取4張隨機照片的另一種方式:

//randomize order 
shuffle($fileList); 
//get first 4 values of array 
$randomPhotos = array_slice($fileList, 0, 4); 

請注意,洗牌將您的原始數組中更改數值順序。