0

因为我在任何地方都找不到答案,也许其他人也对此感到困扰。我想在 backstretch 中使用文件夹中的随机图像,而不指定之前的图像。我不擅长 php,所以有人可以告诉我如何做到这一点,我相信它会是 sth。类似于这个解决方案,它适用于超大(如何使用超大!带有图像文件夹的插件

此外,当重新加载/重新访问页面时,我需要幻灯片以不同的图像开始。

非常感谢你帮助我!!

4

1 回答 1

0

我整理了一个简单的 php 脚本。它从 $image_dir 获取所有 .jpg 并以随机顺序返回它们。

第一个图像被保存为会话变量,刷新后新图像被拾取并添加到数组的开头。

该代码非常基本,如果您有子文件夹,则必须对其进行自定义:

PHP:

<?php
//Start Session
session_start();


function GetRandomImagesFromDir( $image_dir ) {
//Create image array
$images = glob($image_dir . '*.jpg');


//Get first image
    //Get one random image from array
    $rand_key = array_rand($images, 1);

    //Same image as before?
    if ( $rand_key == $_SESSION['last_image'] ) {
        if ( $rand_key > 1 ) {
            $rand_key--;
        } else {
            $rand_key++;
        }
    }

    //Save Key to Session
    $_SESSION['last_image'] = $rand_key;

    //Save image to var
    $first_image = $images[ $rand_key ];


//Get next images
    //Remove first-image from array
    unset( $images[ $rand_key ] );

    //Shuffle
    shuffle( $images );

    //Add first image
    $images[] = $first_image;

    //Reverse array
    $images = array_reverse($images);


//Array to string
    $return_str = implode('","', $images);
    $return_str = '"'.$return_str.'"';


//Return string
return $return_str;
}
?>

JS:

<script>
    $.backstretch([<?=GetRandomImagesFromDir('backstretch_images/')?>]);
</script>
于 2014-06-02T19:31:21.047 回答