显示文件夹中的随机图像,而不重复使用JS或PHP

时间:2012-02-20 08:28:50

标签: php javascript mysql

我想在目录中的文件夹中显示10-15个图像,但我不想重复显示它们。我也不想使用MySQL Table

请帮忙。

目前我正在使用以下代码

             $imglist='';
             $img_folder = "gallerypage/small/";

              mt_srand((double)microtime()*1000);

             $imgs = dir($img_folder);

             while ($file = $imgs->read()) {
                 $imglist .= "$file"."|";

             } closedir($imgs->handle);
             $imglist = explode("|", $imglist); 
             //print_r($imglist);
             $no = sizeof($imglist)-2;
             //echo $no;

             for ($i=0; $i<=$no; $i++)
             {
             $random = $i; // mt_rand($i, $no/$i);
             //echo $random;
             $fileb = 
             $image = $imglist[$i];
             $fileb = $image; 
                 if($image != '.' && $image != '..' && $image != 'Thumbs.db' )
                 {

                    //echo '<img src="'.$img_folder.$image.'" border=0>';
                    //if($image != ""){
                     //echo "k".$image."k";
                    echo "<a href='".$img_folderb.$fileb."' rel='lightbox-journey'><img src='".$img_folder.$image."' title='".$image."' alt='".$image."'   height='100'/>";
                    //}
                 }
             }

4 个答案:

答案 0 :(得分:3)

您可以为图像添加文件夹并迭代,直到达到15,不会重复

$all_images = glob("/your/directory/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);

// shuffle($all_images); // uncomment this line to randomize the images

$images = array();

foreach ($all_images as $index => $image) {
     if ($index == 15) break;  // Only print 15 images
     $image_name = basename($image);
     echo "<img src='/public/directory/{$image_name}' />";
}

答案 1 :(得分:3)

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = '1.gif'
theImages[1] = '2.gif'
theImages[2] = '3.gif'
theImages[3] = '4.gif'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<SCRIPT LANGUAGE="JavaScript">



<!-- Begin
showImage();
//  End -->
</script>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
Master</font>
</center><p>

答案 2 :(得分:1)

$images = glob('/path/to/image/dir/{*.jpg,*.png,*.gif}', GLOB_BRACE);

foreach(array_rand($images,10) as $key) //display 10 image
{
    echo '<img src="'.$images[$key].'" />';
}

答案 3 :(得分:0)

您可以从目录中读取文件并将其存储到数组中,然后将数组洗牌并获取前10项:

$files = array();
$handle=opendir(".");
while (($file = readdir($handle))!==false) {
    if(is_file($file))
       $files[] = $file;
}

shuffle($files);
$ctr = 0;
foreach($files as $f)
{
    //process file here
    if($ctr++>=10)
     break;
}