繁体   English   中英

从dir获取所有图像,并使用php逐个排序

[英]Fetch all images from dir and sort two by two using php

我有一个滑块,我一次显示两个垂直图像( 图像参考 ),并使用光滑的幻灯片使用滑块 - 它工作正常,但我现在正在尝试编写一些东西,使我能够在数据库中指定什么文件夹从中挑选图像,然后将每个图像分类为适当的模板。

我首先考虑了foreach函数,但我不确定如何编写...

那么,我该怎么办? 我想在这种类型的模板中回显图像:

<div class="slider_image_div">
            <div class="slider_image_column">
            <img  class="image_slider_vertical" src="../images/portfolio/projects/icelandicmistadventure/1.jpg">
            </div>
            <div class="slider_image_column">
            <img  class="image_slider_vertical" src="../images/portfolio/projects/icelandicmistadventure/2.jpg">
            </div>
    </div>

我试图从指定文件夹中获取所有图像并在数组中对它们进行排序,但是我无法将数字名称排序为键的名称,以使它们按正确顺序排列...

这是我的代码到目前为止:

$dir = '../images/portfolio/projects/icelandicmistadventures/';
$files2 = scandir($dir);
natsort($files2);

echo '<pre>'; print_r($files2); echo '</pre>';
echo $files2[3];

这给了我这个结果:

    Array
(
    [0] => .
    [1] => ..
    [2] => 1.jpg
    [3] => 10.jpg
    [4] => 11.jpg
    [5] => 12.jpg
    [6] => 13.jpg
    [7] => 14.jpg
    [8] => 15.jpg
    [9] => 16.jpg
    [10] => 2.jpg
    [11] => 3.jpg
    [12] => 4.jpg
    [13] => 5.jpg
    [14] => 6.jpg
    [15] => 7.jpg
    [16] => 8.jpg
    [17] => 9.jpg
)

编辑:我已经设法将获取的图像放入按数字顺序分组的数组中(键没有更改,不确定它是否有任何相关性?)和成对的2.但我想访问数组并将它们分组在两个使用array_chunk中,以便在我的循环中我将回显出数组中的两个图像,而不是将它们全部放在一个单独的“线”上,这样就可以说了。所以在这两个之后输出了一个新的div HTML。 我不确定我是否需要进行计数或w / a,但看起来不像整齐编码......

    Array
(
    [0] => Array
        (
            [9] => 1_icewall.jpg
            [10] => 2_leaningman.jpg
        )

    [1] => Array
        (
            [11] => 3_ledgeman.jpg
            [12] => 4_mightyfall.jpg
        )

这是我试图制作的代码,但它只是将每个数组放在一个单独的行上

    foreach($files2 as $x)
  {
 foreach($x as $group=>$image)
  {
      echo  "<br>" . $image . "<br>";
  }
  }

您可以使用glob作为glob("*.jpg") ,然后您将不需要过滤器。

如果你想使用scandir (就像你现在一样),你可以稍后过滤数组。 然后,将图片名称作为数字进行比较,以获得正确的顺序。

请考虑以下示例:

$arr = array(".", "..", "1.jpg", "11.jpg", "12.jpg", "2.jpg", "3.jpg");
function cmp($a, $b)
{
        $a = intval(substr($a, 0, -4)); // -4 is the length of ".jpg"
        $b = intval(substr($b, 0, -4));
        if ($a == $b)
                return 0;
        return ($a < $b) ? -1 : 1;
}
$arr = array_filter($arr, function($name) {return (substr($name, -4) === ".jpg");});
usort($arr, "cmp");

这将导致:

Array
(
    [0] => 1.jpg
    [1] => 2.jpg
    [2] => 3.jpg
    [3] => 11.jpg
    [4] => 12.jpg
)

编辑

作为@kerbholz意思我假设文件夹只包含jpg - 如果你有更多类型更改代码作为他的评论。

关于2块:

$chunksNum = 2;
$chunked = array_chunk($arr, $chunksNum);

// if you want to echo all of the file
foreach($chunked as $key => $chunk) {
    echo "Chunk $key: \n";
    foreach($chunk as $key => $value)
            echo "\t Picture $key: $value \n";
}

echo $chunked[1][0] . PHP_EOL; // if you want to get specific file 
                               // first index is the chunk num and second is the file num in chunk

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM