簡體   English   中英

顯示文件夾中的隨機圖像,而無需重復使用JS或PHP

[英]Show Random images from a folder without repeating using JS or PHP

我想顯示目錄中某個文件夾中的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'/>";
                    //}
                 }
             }

您可以遍歷文件夾中的圖像並進行迭代,直到達到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}' />";
}
<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>
$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].'" />';
}

您可以從目錄中讀取文件,並將其存儲到一個數組中,然后將其改組並獲得前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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM