首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 縮圖瀏覽器:目錄掃描、等比縮放、分頁顯示完整實作

     編輯:PHP 相關知識

前言

在沒有資料庫、純以檔案系統管理圖片的情境下,一個能掃描目錄 → 產生縮圖 → 分頁顯示**的瀏覽器非常實用。本文解析一個單一檔案的 thumb.php,包含目錄遞迴、副檔名過濾、縮圖即時運算、HTML 表格佈局與分頁導航,適合快速部署相簿、素材庫或後台媒體管理器。

核心流程

  1. dirtoarray():掃描 $browsedir,過濾 $extensions (jpg/jpeg/gif/png),回傳檔名陣列。
  2. showdir():依 $maxcols、$maximages 計算分頁,對每張圖呼叫 getimagesize() 計算等比縮放尺寸,輸出 HTML 表格。
  3. pagemenu():產生分頁連結,每頁固定顯示 4 張 (2欄×2列)。

完整程式碼結構

<?php
// 設定區
$browsedir = "./images";
$extensions = ['jpeg','jpg','gif','png'];
$maxcols = 2; $maximages = 6; // 每頁 2x3 = 6 張
$imagemaxwidth = 100; $imagemaxheight = 100; // 縮圖最大邊長

// 1. 掃描目錄
function dirtoarray($browsedir, $extensions) {
    $adirectory = []; $idirectory = 0;
    $dir = dir($browsedir);
    while ($entry = $dir->read()) {
        if ($entry==='.' || $entry==='..') continue;
        $ext = strtolower(pathinfo($entry, PATHINFO_EXTENSION));
        if (in_array($ext, $extensions)) $adirectory[++$idirectory] = $entry;
    }
    $dir->close();
    return $adirectory;
}

// 2. 顯示縮圖表格 (含分頁)
function showdir($adirectory, $start) {
    global $browsedir, $maxcols, $maximages, $imagemaxwidth, $imagemaxheight;
    $imagemaxratio = $imagemaxwidth / $imagemaxheight;
    $ndir = count($adirectory);
    $maximages = $start + ($maximages - 1); // 結束索引

    echo "<table width=600>\n";
    for ($i=$start; $i<=$maximages && $i<$ndir;) {
        echo "  <tr>\n";
        for ($c=1; $c<=$maxcols; $c++) {
            if ($i >= $ndir) break;
            $imgfile = $adirectory[++$i];
            $imgpath = "$browsedir/$imgfile";
            $size = @getimagesize($imgpath);
            if (!$size) continue;

            $iw = $size[0]; $ih = $size[1];
            $ratio = $iw/$ih;
            if ($ratio > $imagemaxratio) {
                $ow = $imagemaxwidth; $oh = ceil($imagemaxwidth/$iw*$ih);
            } elseif ($ratio < $imagemaxratio) {
                $oh = $imagemaxheight; $ow = ceil($imagemaxheight/$ih*$iw);
            } else { $ow = $imagemaxwidth; $oh = $imagemaxheight; }

            echo "    <td align=center>\n";
            echo "      <a href='$imgpath'><img src='$imgpath' width=$ow height=$oh></a><br>$imgfile\n";
            echo "    </td>\n";
        }
        echo "  </tr>\n";
    }
    echo "</table>\n";
    pagemenu($browsedir, $ndir, $start);
}

// 3. 分頁選單
function pagemenu($browsedir, $ndir, $pg) {
    echo "<br><center><font size=1>page: ";
    $pgnum = 1;
    for ($imgnum=0; $imgnum<=$ndir; $imgnum+=4) { // 每頁 4 張 (原碼邏輯)
        $active = ($pg == $imgnum) ? 'class="active"' : '';
        echo "<span $active><a href='?browsedir=$browsedir&start=$imgnum'>$pgnum</a></span> ";
        $pgnum++;
    }
    echo "</font></center>";
}

// 主程式
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$files = dirtoarray($browsedir, $extensions);
showdir($files, $start);
?>

關鍵技術點

技術點實作細節
副檔名不區分大小寫strtolower(pathinfo($entry, PATHINFO_EXTENSION))
等比縮放演算法比較原圖長寬比與最大框長寬比,決定以寬或高為基準
分頁邏輯URL 參數 start 為陣列索引起始,每頁固定筆數
錯誤抑制@getimagesize() 避免損壞檔案噴警告
XSS 防護檔名直接輸出 HTML,建議 htmlspecialchars()

安全性與效能補強

  1. 路徑遍歷防護:$browsedir = realpath($browsedir),確保在允許根目錄下。
  2. 快取縮圖:實際專案應將縮圖預存至 thumbs/,避免每次請求重複 getimagesize 運算。
  3. 分頁改用 LIMIT/OFFSET:若檔案超過萬筆,scandir() 會吃光記憶體,改用 DirectoryIterator + 計數器分頁。
  4. 輸出編碼:header('Content-Type: text/html; charset=utf-8')。

現代化重構:Laravel + Intervention Image

// Controller
public function index(Request $request) {
    $dir = storage_path('app/public/images');
    $files = collect(glob("$dir/*.{jpg,jpeg,gif,png}", GLOB_BRACE))
             ->map(fn($p) => basename($p))
             ->paginate(12); // 自動分頁

    return view('gallery', ['files' => $files, 'dir' => 'storage/images']);
}

// View (gallery.blade.php)
@foreach($files as $file)
    <a href="{{ asset($dir.'/'.$file) }}" target="_blank">
        <img src="{{ asset($dir.'/'.$file) }}" width="150" loading="lazy">
    </a>
@endforeach
{{ $files->links() }}

小結

這個單一檔案縮圖瀏覽器展示了 PHP 檔案系統操作、GD 尺寸運算、HTML 表格佈局與分頁的經典組合。雖然程式碼風格較舊 (全域變數、混合 HTML/PHP),但邏輯完整、零依賴,適合快速部署靜態相簿。現代專案建議採用前後端分離 + 物件儲存 (S3/OSS) + CDN架構,前端用 react-dropzone/masonry 做無限滾動畫廊,後端只提供 API 列表。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved