記事にするようなことでもないかな・・・とは思いつつ。

 

特定のディレクトリ以下の画像データを一覧表示するスクリプトです。CSSで列の横幅固定していますが、本当はサムネイル作成スクリプトでも書いた方がよかったかも。時間が空いたらそれも書く(かもしれない)。

 

並べ替え(asort)が入っているのは(globの動作として)拡張子ごとにファイルを取得しているみたいで拡張子ごとの並びになってしまったからです(汗

<style>
    table {
    table-layout: fixed;
}
td .tblimg {
    width: 300px
}
</style>


<?php
$tcol = 3; // 列数指定

$images = glob('images/*.{jpg,jpeg,gif,png}', GLOB_BRACE);
asort($images); // 結果を並べ替える(GLOB_BRACEのため必要)
$ct = 0;
foreach ($images as $image) {
    if ($ct === 0) {
        echo '<table>' . PHP_EOL . '<tr>';
    } else if (($ct % $tcol) === 0) {
        echo '</tr>' . PHP_EOL . '<tr>';
    }
    printf('<td><img class="tblimg" src="%s"></td>', $image);
    $ct++;
}
if ($ct > 0) {
    while (($ct % $tcol) > 0) {
        echo "<td></td>";
        $ct++;
    }
    echo '</tr>' . PHP_EOL . '</table>' . PHP_EOL;
} else {
    echo 'NO DATA<br>';
}

コメント
コメントする