PHP 从文件夹中随机拉取图像


PHP pull random image from folder

我想知道从文件夹中提取随机图像的"更好"方法。

就像说,要让 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 = ''; 
    }
?>

试试这个:

<?php
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?>
<img src="images/<?php echo $images[$i]; ?>" alt="" />

下面的代码通过图像扩展验证图像列表。

 <?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; ?>
 
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');

将"图像"替换为您的文件夹。

我在互联网上搜索了几个小时,以实现我想要的代码。我把我在网上找到的各种答案放在一起。这是代码:

<?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(因为glob()对我不起作用),我能够为轮播画廊提取 5 张图像,并摆脱重复的图像并整理空值。使用我的方法的一个缺点是,图库中的图像计数在 3 到 5 张图像之间变化,可能是由于删除了空值。这并没有打扰我太多,因为它可以根据需要工作。如果有人能使我的方法更好,我欢迎你这样做。工作示例(网站顶部的第一个轮播画廊):伊斯特菲尔德细木工