2012-04-21 59 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

相关问题