dongluan5740 2017-09-26 22:01
浏览 89

PHP显示目录中的随机图像

I am kinda new to PHP.

I am searching a way to make PHP showing 6 random JPG images from a directory, no matter what the imagename is. The other important thing is that all the displayed images should be different.

Do you have any idea what's the easiest way to do it? I know i should use the glob function, but how to limit the images to 6,random and non repeatable?

I have that as code for the moment:

<?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>'; 
}  
?>          

But on image refresh it's not displaying different images, it's just tooking random 6 and keep displaying them all the time.

  • 写回答

1条回答 默认 最新

  • dousi6405 2017-09-26 22:14
    关注

    Files: (Random names used)

    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);
        }
    }
    
    ?>
    

    Credits for rotator script http://old.marcofolio.net/webdesign/php_random_image_rotation.html

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真