2017-09-26 83 views
-6

我對PHP有點新鮮。PHP從目錄中顯示隨機圖像

我正在尋找一種方法來讓PHP顯示來自目錄的6個隨機JPG圖像,無論圖像名是什麼。另一個重要的是所有顯示的圖像應該不同。

你知道什麼是最簡單的方法嗎? 我知道我應該使用glob函數,但如何將圖像限制爲6,隨機且不可重複?

我有作爲的時刻代碼:

<?php 
    $pictures = glob("images/gallery/*.jpg"); 
    $no_pictures = count($pictures)-1; 
    $limit = $no_pictures-5;    
    for($i = $no_pictures; $i >= $limit; $i--){ 
    echo '<div class="col-md-4 col-sm-6 col-xs-12 wow fadeInUp"> 
      <a class="thumb" href="'.$pictures[$i].'"><img src="'.$pictures[$i].'" alt="Gallery"/><span class="thumb_overlay"></span></a> 
      </div>'; 
} 
?>   

但在圖像刷新它沒有顯示不同的圖像,它只是tooking隨機6並保持顯示他們所有的時間。

+1

開始將HTTP ://php.net/manual/en/function.array-rand.php – rtfm

回答

-1

文件:(使用隨機的名字)

images/header/rotate.php 
images/header/abstract.jpg 
images/header/bird.jpg 
images/header/butterfly.jpg 
images/header/happy.jpg 

HTML:

<img src="images/header/rotate.php" alt="Header" width="400" height="100" /> 

PHP:

<?php 

/* 
Check it out for more interesting scripts & downloads 

AUTOMATIC IMAGE ROTATOR 
Version 2.2 - December 4, 2003 
Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd. 
All Rights Reserved. 
http://www.hiveware.com/imagerotator.php 

*/ 

$folder = '.'; 


$extList = array(); 
$extList['gif'] = 'image/gif'; 
$extList['jpg'] = 'image/jpeg'; 
$extList['jpeg'] = 'image/jpeg'; 
$extList['png'] = 'image/png'; 


// You don't need to edit anything after this point. 


// --------------------- END CONFIGURATION ----------------------- 

$img = null; 

if (substr($folder,-1) != '/') { 
$folder = $folder.'/'; 
} 

if (isset($_GET['img'])) { 
$imageInfo = pathinfo($_GET['img']); 
if (
    isset($extList[ strtolower($imageInfo['extension']) ]) && 
     file_exists($folder.$imageInfo['basename']) 
    ) { 
     $img = $folder.$imageInfo['basename']; 
    } 
    } else { 
    $fileList = array(); 
    $handle = opendir($folder); 
    while (false !== ($file = readdir($handle))) { 
    $file_info = pathinfo($file); 
    if (
     isset($extList[ strtolower($file_info['extension']) ]) 
    ) { 
     $fileList[] = $file; 
    } 
} 
closedir($handle); 

if (count($fileList) > 0) { 
    $imageNumber = time() % count($fileList); 
    $img = $folder.$fileList[$imageNumber]; 
    } 
} 

if ($img!=null) { 
    $imageInfo = pathinfo($img); 
    $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ]; 
     header ($contentType); 
    readfile($img); 
} else { 
if (function_exists('imagecreate')) { 
    header ("Content-type: image/png"); 
    $im = @imagecreate (100, 100) 
     or die ("Cannot initialize new GD image stream"); 
    $background_color = imagecolorallocate ($im, 255, 255, 255); 
    $text_color = imagecolorallocate ($im, 0,0,0); 
    imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color); 
    imagepng ($im); 
    imagedestroy($im); 
    } 
} 

?> 

學分轉子腳本 http://old.marcofolio.net/webdesign/php_random_image_rotation.html

+0

14年前的代碼,可能不是最好的想法 – rtfm

+0

@rtfm可能是更優化的代碼來做同樣的事情,但我用它和它確實有效,不會殺死那些糟糕的服務器資源,不管圖像的明顯加載情況如何,無論採取哪種方式,都會發生。 –