如何使用PHP从目录中获取随机图像

时间:2009-11-19 06:20:26

标签: php

我有一个名为images / tips的目录。

现在在该目录中我有许多可以改变的图像。

我希望PHP脚本能够读取目录,查找图像以及找到随机图像的图像。

关于如何做到这一点的任何想法?

10 个答案:

答案 0 :(得分:56)

$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments

您可以向array_rand()发送第二个参数以获得超过1个。

答案 1 :(得分:4)

$images = glob('images/tips/*');
return $images[rand(0, count($images) - 1)];

但是,这并不能确保不会连续两次拾取相同的图像。

答案 2 :(得分:1)

<?php
   foreach (glob("gallery/*") as $filename) {
         echo '<li><a href="'.$filename.'" title=""><img src="'.$filename.'" alt="" /></a> </li>';      
       }
?>

查看此代码,如果对您有用,请务必使用它。它从文件夹加载所有文件并以上述格式打印它们。我将此代码用于灯箱。

答案 3 :(得分: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');

替换&#39;图片&#39;与您的文件夹。

答案 4 :(得分:1)

同意alexa。 使用简单的功能。

function RandImg($dir)
{
$images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)];
return $randomImage;
}

$the_image = RandImg('images/tips/');
echo $the_image;

答案 5 :(得分:0)

您可以使用opendir()从该目录读取文件名,将每个文件名存储在一个数组中。然后使用rand(),使用与数组键对应的min和max来从数组中选择一个项目。

答案 6 :(得分:0)

更简单:

$directory = "medias/photos/";
$img = glob($directory . "*.jpg");
shuffle($img);

答案 7 :(得分:0)

$folder = "images";
$results_img_arr = array();
if (is_dir($folder))
{
        if ($handle = opendir($folder))
        {
                while(($file = readdir($handle)) !== FALSE)
                {
                    if(!in_array($file,array(".","..")))
                        $results_img_arr[] = $folder."/".$file;
               }
         closedir($handle);
        }
}
$ran_img_key  = array_rand($results_img_arr);

$img_path = $results_img_arr[$ran_img_key];

答案 8 :(得分:0)

在您的Web服务器的根目录中创建一个文件夹“ php”,并将该文件php“ rotate.php”放入其中...

<?php
  ###############################################
  # Simple Php Image Rotator - 1.1 - 24.10.2018 #
  # Alessandro Marinuzzi - http://www.alecos.it #
  ###############################################
  function rotate($folder) {
    $list = scandir($folder);
    $fileList = array();
    $img = '';
    foreach ($list as $file) {
      if (is_file($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;
  }
?>

用法(例如,在root用户中,您可能有一个包含图片的“ pic”文件夹):

<a href="/<?php include("php/rotate.php"); echo rotate("pic"); ?>">

使用高幻灯片库的其他用法:

<a href="/<?php include("php/rotate.php"); echo rotate("pic"); ?>" class="highslide" onclick="return hs.expand(this)"><img src="/<?php echo rotate("pic"); ?>" title="Click To Enlarge" alt="Random Gallery" width="90" height="67"></a>

答案 9 :(得分:-1)

使用图片加载文件夹:

$folder = opendir(images/tips/);

从目录中构建文件/图像表:

$i = 0;
while(false !=($file = readdir($folder))){
if($file != "." && $file != ".."){
    $images[$i]= $file;
    $i++;
    }
}

随机选择:

$random_img=rand(0,count($images)-1);

在页面上显示:

echo '<img src="images/tips'.$images[$random_img].'" alt="" />';

希望它有所帮助。当然将其括在<?php ?>

相关问题