【问题标题】:Extracting images in a certain way以某种方式提取图像
【发布时间】:2015-09-12 16:48:58
【问题描述】:

我有一个自定义的滑块 (flexslider),因此每张幻灯片显示 10 个图像。该设计以手动插入图像源的方式构建。现在我正在尝试优化滑块,以便使用 php 动态提取图像。我设法做到了,但是每张幻灯片只出现一个图像。对每张幻灯片显示 10 张不同的图像有任何帮助吗?

滑块代码: JSFIDDLE:https://jsfiddle.net/atkumqpk/1/

HTML:

<div id="menu" class="menu">
    <ul class="headlines">
        <li id="item1">
            <button>aaaaaaaa</button>
        </li>
        <li id="item2">
            <button>bbbbbbb</button>
        </li>
        <li id="item3">
            <button>ccccccc</button>
        </li>
        <li id="item4">
            <button>dddddddd</button>
        </li>
        <li id="item5">
            <button>eeeeeee eee.</button>
        </li>
        <li id="item6">
            <button>ffffff</button>
        </li>
        <li id="item7">
            <button>ggggggg</button>
        </li>
    </ul>
</div>
 <div id="container">
<div id="first" class="inner-container">
   <div id="item11" class="item"> <a name="item11"></a>

                <div class="flexslider">
  <ul class="slides">
    <li>
                <img id="image1" src="http://placehold.it/350x150" />
                <img id="image2" src="http://placehold.it/350x150" />
                <img id="image3" src="http://placehold.it/350x150" />
                <img id="image4" src="http://placehold.it/350x150" />
                <img id="image5" src="http://placehold.it/350x150" />
                <img id="image6" src="http://placehold.it/350x150" />
                <img id="image7" src="http://placehold.it/350x150" />
                <img id="image8" src="http://placehold.it/350x150" />
                <img id="image9" src="http://placehold.it/350x150" />
                <img id="image10" src="http://placehold.it/350x150" />
    </li>
    <li>
       <img id="image1" src="http://placehold.it/350x150" />
                <img id="image2" src="http://placehold.it/350x150" />
                <img id="image3" src="http://placehold.it/350x150" />
                <img id="image4" src="http://placehold.it/350x150" />
                <img id="image5" src="http://placehold.it/350x150" />
                <img id="image6" src="http://placehold.it/350x150" />
                <img id="image7" src="http://placehold.it/350x150" />
                <img id="image8" src="http://placehold.it/350x150" />
                <img id="image9" src="http://placehold.it/350x150" />
                <img id="image10" src="http://placehold.it/350x150" />
    </li>
    <li>
       <img id="image1" src="http://placehold.it/350x150" />
                <img id="image2" src="http://placehold.it/350x150" />
                <img id="image3" src="http://placehold.it/350x150" />
                <img id="image4" src="http://placehold.it/350x150" />
                <img id="image5" src="http://placehold.it/350x150" />
                <img id="image6" src="http://placehold.it/350x150" />
                <img id="image7" src="http://placehold.it/350x150" />
                <img id="image8" src="http://placehold.it/350x150" />
                <img id="image9" src="http://placehold.it/350x150" />
                <img id="image10" src="http://placehold.it/350x150" />
    </li>
  </ul>
</div>
            </div>
        </div>

用于提取图像的 PHP 代码:

        $folder = 'images'; // chose folder
        $extensions = array('JPG','jpg','jpeg','gif','png'); // allowed extensions
    $slika = scandir($folder); // scan folder
    sort($slika); // sort
    foreach($slika as $key => $value) {       
            $get_extensions = explode(".",$value);
            $ext = $get_extensions[count($get_extensions) - 1];
            if (in_array($ext, $extensions))
            {
            $title = substr($value, 0,strrpos($value,'.')); // image name
            echo "<li><img src='".$folder."/".$value."' /></li>"; 
            }
    }

【问题讨论】:

    标签: php html css


    【解决方案1】:

    这应该根据指定的folder 返回一个以 10 个为单位的数组。

    function get_slide_images($folder, $images_per_slide = 10)
    {
    
        $slide_images = false;
    
        if (file_exists($folder)) {
    
            // valid extensions
            $extensions = array(
                "jpg",
                "gif",
                "jpeg",
                "svg",
                "png",
                "bmp"
            );
    
            foreach (new DirectoryIterator($folder) as $file_key => $file) {
    
                // Don't bother
                if (!in_array($file->getExtension(), $extensions)) {
                    continue;
                }
    
                // Grab file details
                $filename    = $file->getFilename();
                $file_folder = $folder . "/" . $filename;
    
                // Store the image to the Slide
                $slide_images[$filename] = "<img src='{$file_folder}' alt='{$file_folder}' />";
    
            }
    
            if (!empty($slide_images)) {
                ksort($slide_images);
                $slide_images = array_chunk($slide_images, $images_per_slide);
            }
    
        }
        return $slide_images;
    }
    

    用法

    $slider_kvp = get_slide_images("images", 10);
    
    echo "<pre>";
    var_dump($slider_kvp); // This will tell you whats in our slider
    echo "</pre>";
    
    /**
    * Here we are going to generate the SLIDES
    */
    if($slider_kvp) {
    
        $slider_list_html = array();
    
        foreach($slider_kvp as $slider_key => $slide_images) {
    
            $html_LI_list = "";
            $html_LI_list = "<li>";
    
            // Go through each image ...
            foreach($slide_images as $image_key => $image_value) {
                $html_LI_list .= $image_value;
            }
    
            $html_LI_list .= "</li>";
    
            $slider_list_html[$slider_key] = $html_LI_list;
    
        }
    
        // OUR SLIDES!
        $rendered_slider_list_html = implode(' ', $slider_list_html);
        echo "<ul class='slides'>{$rendered_slider_list_html}</ul>";
    
    }
    

    【讨论】:

    • 亲爱的我尝试了你的代码,但我的滑块和图像消失了。我是 php 新手,也许我在我的代码中放置了错误的函数? jsfiddle:jsfiddle.net/p54Lfzks
    • 我已经更新了我的答案,以提供一些基本的使用细节。
    • 我已经更新了第二部分,基本上就是你的幻灯片。
    • 您能否在我的帖子中提供的 jsfiddle 中告诉我,如何放置您的代码?我刚开始用php(就像2天前)
    • 你不能在 JSFiddle 中运行 PHP
    猜你喜欢
    • 2019-12-15
    • 1970-01-01
    • 2020-12-23
    • 2021-05-22
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多