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?

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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面