随机化幻灯片图像

时间:2011-10-12 15:04:47

标签: php drupal slideshow swfobject

我有这个drupal幻灯片,它按照图像标题(01_title.jpg,02_title.jpg等)顺序从文件夹中提取图像。

我想知道是否有一种简单的方法来随机化图像,所以每次刷新页面时都会以不同的图像开始?

您可以在此处查看幻灯片显示http://www.rubensteinpr.com/

谢谢!

<div id ="index">
        <?php
            // Note that !== did not exist until 4.0.0-RC2
            $desired_extension = 'jpg'; //extension we're looking for 
            $banner_imgs_array = array(); // array of banner images
            $banner_imgs = ''; // sting of banner images names comma dileneated

            if ($handle = opendir(file_directory_path().'/banner_imgs')) {

                /* This is the correct way to loop over the directory. */
                while (false !== ($file = readdir($handle))) {
                    if(($file != ".") and ($file != "..")) { 
                      $fileChunks = explode(".", $file); 
                      if($fileChunks[1] == $desired_extension) //interested in second chunk only 
                      {       
                        $banner_imgs_array[] = $file;
                      } 
                    } 
                }

                closedir($handle);

                $banner_imgs = implode(',', $banner_imgs_array);
            }
        ?>
        <div id="banner"><img src="<?php print file_directory_path(); ?>/temp_banner.jpg" width="702" height="310" border="0"></div>
        <div id="bannerText">media relations &bull; strategic planning &bull; digital communications &bull; crisis management</div>
        <script type="text/javascript">
            // <![CDATA[

            var so = new SWFObject("<?php print file_directory_path(); ?>/banner.swf", "ban", "702", "310", "8", "#ffffff");
            so.addParam('menu', 'false');
            so.addParam("wmode", "transparent");
            so.addParam("base", "<?php print file_directory_path(); ?>");
            so.addVariable("banner_imgs", "<?php print $banner_imgs; ?>");
            so.write("banner");

            // ]]>
        </script>
</div>

2 个答案:

答案 0 :(得分:2)

添加

shuffle($banner_imgs_array);

之前

$banner_imgs = implode(',', $banner_imgs_array);

应该这样做。

答案 1 :(得分:0)

array_rand将返回一个或多个随机数组键。如果要对阵列本身进行随机播放,请使用shuffle