2017-09-26 22 views
-6

私はPHPを少し熟知しています。ディレクトリからランダムな画像を表示する

私は、imagenameが何であるかに関係なく、ディレクトリから6つのランダムJPG画像を表示するPHPを作る方法を探しています。もう一つの重要なことは、表示されるすべての画像が異なっていることです。

これを行う最も簡単な方法は何ですか。 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>'; 
} 
?>   

しかし、画像の上に、それは別の画像を表示していないリフレッシュし、それだけでランダム6をtookingし、それらのすべての時間を表示し続けるです。

+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おそらく同じことをするために最適化されたコードですが、それはうまくいかないことが起こっているイメージの明白な読み込みを除いて、悪いサーバーリソースを殺すことはありません。 –

関連する問題