簡體   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