2012-04-21 39 views
6

我想知道從文件夾中拉出隨機圖像的「更好」方式。從文件夾中拉出隨機圖像

就像說,讓PHP只是從文件夾中選擇一個隨機圖像,而不是搜索和創建一個數組。

這裏是我今天做

<?php 
    $extensions = array('jpg','jpeg'); 
    $images_folder_path = ROOT.'/web/files/Header/'; 
    $images = array(); 
    srand((float) microtime() * 10000000); 

    if ($handle = opendir($images_folder_path)) { 
     while (false !== ($file = readdir($handle))) { 
      if ($file != "." && $file != "..") { 
       $ext = strtolower(substr(strrchr($file, "."), 1)); 
       if(in_array($ext, $extensions)){ 
       $images[] = $file; 
       } 
      } 
     } 
    closedir($handle); 
    } 
    if(!empty($images)){ 
     $header_image = $images[array_rand($images)]; 
    } else { 
     $header_image = ''; 
    } 
?> 

回答

11

試試這個:通過圖像擴展

<?php 

$dir = "images/"; 
$images = scandir($dir); 
$i = rand(2, sizeof($images)-1); 
?> 

<img src="images/<?php echo $images[$i]; ?>" alt="" /> 
+0

**您的答案很好,很簡短!**但還有一個問題! 如果圖像目錄包含大量圖像(1K),[scandir](http://php.net/manual/en/function.scandir.php)會將所有圖像加載到數組中。 雖然你只需要一個圖像。有沒有辦法改善你的答案?如果你可以用一個選項來包裝它,以獲得有限數量的圖像。即'$ returned_images_count = 10;',我的意思是在[這個問題]中部分解釋(https://stackoverflow.com/q/10642777/7735285)。 +'opendir'在分析過程中看起來更快。 – wpcoder 2017-10-30 19:32:59

1

下面的代碼驗證圖像列表。

<?php function validImages($image) { $extensions = array('jpg','jpeg','png','gif'); if(in_array(array_pop(explode(".", $image)), $extensions)) { return $image; } } $images_folder_path = ROOT.'/web/files/Header/'; $relative_path = SITE_URL.'/web/files/Header/'; $images = array_filter(array_map("validImages", scandir($images_folder_path))); $rand_keys = array_rand($images,1); ?> <?php if(isset($images[$rand_keys])): ?> <img src="<?php echo $relative_path.$images[$rand_keys]; ?>" alt="" /> <?php endif; ?>
1
function get_rand_img($dir) 
{ 
    $arr = array(); 
    $list = scandir($dir); 
    foreach ($list as $file) { 
     if (!isset($img)) { 
      $img = ''; 
     } 
     if (is_file($dir . '/' . $file)) { 
      $ext = end(explode('.', $file)); 
      if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png' || $ext == 'GIF' || $ext == 'JPEG' || $ext == 'JPG' || $ext == 'PNG') { 
       array_push($arr, $file); 
       $img = $file; 
      } 
     } 
    } 
    if ($img != '') { 
     $img = array_rand($arr); 
     $img = $arr[$img]; 
    } 
    $img = str_replace("'", "\'", $img); 
    $img = str_replace(" ", "%20", $img); 
    return $img; 
} 


echo get_rand_img('images'); 

用您的文件夾替換'圖像'。

0

我在互聯網上搜索了幾個小時來實現我想要的代碼。我把我在網上找到的各種答案放在一起。下面是代碼:

<?php 
    $folder = opendir("Images/Gallery Images/"); 
    $i = 1; 
    while (false != ($file = readdir($folder))) { 
     if ($file != "." && $file != "..") { 
      $images[$i] = $file; 
      $i++; 
     } 
    } 
    //This is the important part... 
    for ($i = 1; $i <= 5; $i++) { //Starting at 1, count up to 5 images (change to suit) 
     $random_img = rand(1, count($images) - 1); 
     if (!empty($images[$random_img])) { //without this I was sometimes getting empty values 
      echo '<img src="Images/Gallery Images/' . $images[$random_img] . '" alt="Photo ' . pathinfo($images[$random_img], PATHINFO_FILENAME) . '" />'; 
      echo '<script>console.log("' . $images[$random_img] . '")</script>'; //Just to help me debug 
      unset($images[$random_img]); //unset each image in array so we don't have double images 
     } 
    } 
?> 

使用這種方法,我能夠落實執行opendir沒有任何錯誤(如​​3210沒有工作對我來說),我能拉5張圖片輪播的畫廊,並擺脫重複的圖像和清理空值。使用我的方法的一個缺點是圖像數量在圖庫中的3到5張圖像之間變化,可能是由於空值被刪除。這並沒有打擾我太多,因爲它根據需要運作。如果有人能使我的方法更好,我歡迎你這樣做。 工作示例(網站頂部的第一個輪播展廊):Eastfield Joinery

相關問題