PHP多重随机图像调用
我试图从一个文件夹一次调用多个图像到屏幕,目前我编写的这个PHP只调用一个。如果您有任何想法,我们将不胜感激PHP多重随机图像调用,php,image,Php,Image,我试图从一个文件夹一次调用多个图像到屏幕,目前我编写的这个PHP只调用一个。如果您有任何想法,我们将不胜感激 <?php // Indicate the location of your images $root = ''; // use if specifying path from root $path = 'images/'; function getImagesFromDir($path) { $images = array(); if ( $img_dir
<?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);
?>
这不是很优雅的方法,但对您来说是个好的开始
$imageCount = 3; // show 3 images
$img = array();
for ($i=0; $i<$imageCount; $i++){
$img[] = getRandomFromArray($imgList);
}
$imageCount=3;//显示3个图像
$img=array();
对于($i=0;$i),实际问题是什么。是否出现问题?如果出现问题,原因是什么?您的代码是否不完整?如果出现问题,原因是什么?等等。嗨,Joe,欢迎来到StackOverflow。请仔细阅读“”。太好了,谢谢你的快速回复,对不起,我是PHP的新手,getRandomFromArray是不是比我目前的技术更值得研究。在开始编写任何代码之前,你应该知道算法。这样想:你有一些图片数组。你怎么能随机选取X个图片,这样它们就不会重复?在保存所选图像的位置创建另一个数组。现在从“图像”数组中提取一个尚未在“所选”数组中的图像。然后执行X次。完成。现在用任何您想要的编程语言编写。如果您不具备基本知识(不知道如何编写If语句、循环等),则应该从这开始。然后你可以继续用你想要的语言去做。祝你好运:)