drnbotxo449747 2016-03-11 14:36
浏览 46
已采纳

PHP多个随机图像调用

i am trying to call more than one image to the screen at a time from a folder, currently this PHP i have coded only calls one. Any ideas would be very much appreciated...

<?php


// Indicate the location of your images 
$root = '';
// use if specifying path from root
$path = 'images/';

function getImagesFromDir($path) {
    $images = array();
    if ( $img_dir = @opendir($path) ) {
        while ( false !== ($img_file = readdir($img_dir)) ) {
            // Checks for file formats
            if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
                $images[] = $img_file;
            }
        }
        closedir($img_dir);
    }
    return $images;
}

function getRandomFromArray($ar) {
    mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
    $num = array_rand($ar);
    return $ar[$num];
}


// Collects list of images from directory 
$imgList = getImagesFromDir($root . $path);

$img = getRandomFromArray($imgList);
?> 
  • 写回答

1条回答 默认 最新

  • doqvzh345334 2016-03-11 14:43
    关注

    Not very elegant method, but it's good start for you.

    $imageCount = 3; // show 3 images
    $img = array();
    for ($i=0; $i<$imageCount; $i++){    
       $img[] = getRandomFromArray($imgList);
    }
    

    And now you have three random images inside your $img array. You can additionally check if you don't have the same images.

    There are many ways to achieve your task, maybe introduce that functionality inside getRandomFromArray method?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页