【问题标题】:PHP images from directory - Random order目录中的 PHP 图像 - 随机顺序
【发布时间】:2012-09-21 21:10:29
【问题描述】:

我的网站有一个页面,用于存储参考图像..

目前我只是将所有图像放到我服务器上的一个目录中,然后 php 会以我喜欢的方式显示它们。

我想问的是如何让它们在每次刷新页面时以不同的随机顺序显示?

代码如下:

$dir = 'images';
$file_display = array ('jpg', 'jpeg', 'png', 'gif');


if (file_exists($dir) ==false) {
echo 'Directory \'', $dir, '\' not found';
} else {
$dir_contents = scandir($dir);


foreach ($dir_contents as $file) {
    $file_type = strtolower(end(explode('.', $file)));

    if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
    echo '<img class="photo" src="', $dir, '/', $file, '" alt="', $file, '" />';
    }
}
}

【问题讨论】:

    标签: php image random directory


    【解决方案1】:

    为了保证每次的顺序都不同,您需要携带有关它们在页面加载之间的显示顺序的数据。 但是,这不一定是您需要的 - 如果您只是简单地每次随机排序,那么目录中的图像数量越多,您获得相同订单两次的机会就越低。

    您可以简单地使用shuffle() 来随机化数组的顺序:

    $dir = 'images';
    $file_display = array ('jpg', 'jpeg', 'png', 'gif');
    
    if (file_exists($dir) == false) {
        echo 'Directory \'', $dir, '\' not found';
    } else {
        $dir_contents = scandir($dir);
        shuffle($dir_contents);
    
        foreach ($dir_contents as $file) {
            $file_type = strtolower(end(explode('.', $file)));
    
            if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
                echo '<img class="photo" src="', $dir, '/', $file, '" alt="', $file, '" />';
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      看看洗牌功能。 http://php.net/manual/en/function.shuffle.php 由于 PHP 是无状态的,您要么每次重新扫描您的目录,要么将 $dir_contents 分配给会话变量。然后你可以简单地打乱会话变量。

       if ($file !== '.' && $file !== '..' && in_array($file_type, suffle($file_display)) == true) {
      

      试试看。

      【讨论】:

        【解决方案3】:

        scandir创建的图片数组使用php shuffle

         $dir = 'images';
         $file_display = array ('jpg', 'jpeg', 'png', 'gif');     
         if (file_exists($dir) == false) {
            echo 'Directory \'', $dir, '\' not found';
         } else {
            $dir_contents = scandir($dir);                    
            if(shuffle($dir_contents)) {                    
              foreach ($dir_contents as $file) {
                 $info = new SplFileInfo($file);
        
              // scandir returns an array of files and,or directories 
              // so we should check if $file is a file 
              // and that it's extension matches the allowed ones 
              // which are ('jpg', 'jpeg', 'png', 'gif')
        
                 if(is_file($file) && in_array($info->getExtension(),$file_display)) {
                    echo '<img class="photo" src="', $dir, '/', $file, '" alt="', $file, '" />';                                  
                  }          
              }        
            } else {
            echo 'Error applying random order!';
         }
        }
        

        【讨论】:

          【解决方案4】:

          请按照以下说明操作:在您网站的根目录中创建一个文件夹 "php" 并放入以下 ​​php 文件 rotate.php... 现在在您的根目录中创建一个文件夹 "pic""xmas"...您可以通过编辑 var $my_folder_holiday$my_folder_default 来选择文件夹名称或其他内容...

          <?php
            ##########################################################
            # Simple Script Random Images Rotator • 1.4 • 04.01.2020 #
            # Alessandro Marinuzzi [alecos] • https://www.alecos.it/ #
            ##########################################################
            function rotate($folder) {
              if ((file_exists($_SERVER['DOCUMENT_ROOT'] . "/$folder")) && (is_dir($_SERVER['DOCUMENT_ROOT'] . "/$folder"))) {
                $list = scandir($_SERVER['DOCUMENT_ROOT'] . "/$folder");
                $fileList = array();
                $img = '';
                foreach ($list as $file) {
                  if ((file_exists($_SERVER['DOCUMENT_ROOT']  . "/$folder/$file")) && (is_file($_SERVER['DOCUMENT_ROOT']  . "/$folder/$file"))) {
                    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                    if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') {
                      $fileList[] = $file;
                    }
                  }
                }
                if (count($fileList) > 0) {
                  $imageNumber = time() % count($fileList);
                  $img = $folder . '/' . $fileList[$imageNumber];
                }
                return $img;
              } else {
                mkdir($_SERVER['DOCUMENT_ROOT'] . "/$folder", 0755, true);
              }
            }
            $my_gallery_month = date('m');
            $my_folder_default = 'pic';
            $my_folder_holiday = 'xmas';
            if ($my_gallery_month == 12) {
              $my_gallery = rotate($my_folder_holiday);
            } else {
              $my_gallery = rotate($my_folder_default);
            }
          ?>
          

          使用非常简单,并且在 PHP 7.4 下运行良好...如果您的网站根目录中有一个文件夹 "pic""xmas" 包含您的图像,请放入您的 index.php(或其他文件 php 位于根目录):

          <a href="/<?php include("php/rotate.php"); echo $my_gallery; ?>"><img src="/<?php echo $my_gallery; ?>" alt="Random Gallery" width="90" height="67"></a>
          

          这是使用FancyBox库的另一种用法:

          <a href="/<?php include("php/rotate.php"); echo $my_gallery; ?>" data-fancybox><img src="/<?php echo $my_gallery; ?>" alt="Random Gallery" width="90" height="67"></a>
          

          希望这会有所帮助...

          【讨论】:

            猜你喜欢
            • 2018-03-08
            • 2012-08-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-04-16
            • 2011-06-18
            • 2013-02-04
            • 1970-01-01
            相关资源
            最近更新 更多