PHP随机图像,不重复

时间:2017-06-10 19:09:49

标签: javascript php arrays image random

看看这个网址:
http://www.portaloficial.com.br/teste/teste1/demo.php
我需要这些图像是随机的,而不是重复,每张专辑 有人可以帮我这个吗? .................................................. ....................

以下是代码:

if (!isset($_GET['album'])) {
    // display list of albums
    $folders = scandir($mainFolder, 0);
    $ignore  = array('.', '..', 'thumbs');
    $albums = array();
    $captions = array();
    $random_pics = array();
    foreach($folders as $album) {
        if(!in_array($album, $ignore)) {
            array_push( $albums, $album );
            $caption = substr($album,0,40);
            array_push( $captions, $caption );
            $rand_dirs = glob($mainFolder.'/'.$album.'/thumbs/*.*', GLOB_NOSORT);
            $rand_pic = $rand_dirs[array_rand($rand_dirs)];
            array_push( $random_pics, $rand_pic );
        }
    }
    if( count($albums) == 0 ) {
        echo 'Não há álbuns.';
    } else {
        $numPages = ceil( count($albums) / $albumsPerPage );
        if(isset($_GET['p'])) {
            $currentPage = $_GET['p'];
            if($currentPage > $numPages) {
                $currentPage = $numPages;
            }
        } else {
            $currentPage=1;
        }
        $start = ( $currentPage * $albumsPerPage ) - $albumsPerPage;
        for( $i=$start; $i<$start + $albumsPerPage; $i++ ) {
            if( isset($albums[$i]) ) {
                echo '<div class="thumb-album shadow" style="width:100%; text-align:center;">
                        <div class="thumb-wrapper">
                            <a href="'.$_SERVER['PHP_SELF'].'?album='. urlencode($albums[$i]) .'" style="padding:5px;">
                                <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />
                                <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />
                                <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />
                                <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />
                                <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />
                                <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />
                            </a>
                        </div>
                        <a href="'.$_SERVER['PHP_SELF'].'?album='. urlencode($albums[$i]) .'">
                            <span>'. $captions[$i] .'</span>
                        </a>
                    </div>
                    <div class="clear"></div>';
            }
        }
        echo '<div class="clear"></div>';
        echo '<div align="center" class="paginate-wrapper">';
            $urlVars = "";
            print_pagination($numPages,$urlVars,$currentPage);
        echo '</div>';
    }

1 个答案:

答案 0 :(得分:0)

变化: <img src="'. $random_pics[$i] .'" width="'.$thumb_width.'" alt="" />

<img src="'. $random_pics[mt_rand(0, count($random_pics) - 1)] .'" width="'.$thumb_width.'" alt="" />

虽然很有可能在显示时获得欺骗,但如果你不想要,那么建立一个特定于图像集的数组。