从随机文件夹中选择随机图像

时间:2018-11-14 16:41:57

标签: php image random directory

我正在尝试从随机目录中选择随机图像。我使函数可以获取随机目录,而另一个函数可以从该目录获取随机图像:

<?php

function showRandomDir()
    {
    $files = glob('images/portfolio/*/', GLOB_ONLYDIR);
    shuffle($files);
    $files = array_slice($files, 0, 1);
    foreach($files as $file)
        {
        return $file;
        }
    }

function rotate2()
    {
    $dir = showRandomDir();
    $list = scandir($dir);
    $fileRotateList = array();
    $img = '';
    foreach($list as $fileRotate)
        {
        if (is_file($dir . htmlspecialchars($fileRotate)))
            {
            $ext = strtolower(pathinfo($fileRotate, PATHINFO_EXTENSION));
            if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png')
                {
                $fileRotateList[] = $fileRotate;
                }
            }
        }

    if (count($fileRotateList) > 0)
        {
        $imageNumber = time() % count($fileRotateList);
        $img = $dir . urlencode($fileRotateList[$imageNumber]);
        }

    return $img;
    }

尝试调用rotate2函数时,输出仅是随机目录,而没有图像名称:

电流输出: / images / portfolio / randomDir

应为: /images/portfolio/randomDir/randomImage.jpg

0 个答案:

没有答案