【问题标题】:Pull images from folder for Backstretch从文件夹中提取图像以进行 Backstretch
【发布时间】:2014-06-02 16:26:13
【问题描述】:

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

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

非常感谢你帮助我!!

【问题讨论】:

    标签: php image directory reload jquery-backstretch


    【解决方案1】:

    我已经整理了一个简单的 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>
    

    【讨论】:

    • 嘿维克多,非常感谢,它工作得很好,除了它只显示一张图片,这意味着幻灯片不会继续。我在做某事吗?错了吗?
    • 如果我这样说它有效,那是你想要的吗? ' '
    • 我已经编辑了代码,它现在应该返回所有图像并在每次刷新后替换第一个。
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多