萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 實作自動輪播圖片展示 (SlideShow 類別)

功能簡介

一個完整的 PHP 圖片輪播類別,掃描指定目錄下的圖片 (jpg/jpeg/gif/png),使用 Session 記錄圖片順序,支援首頁、上一張、下一張、尾頁導航,內建 CSS 樣式,可直接部署為獨立頁面。

核心類別結構

<?php
/* PHP Image Slideshow - Auto Version - PHP5 */

define('IMGDIR', '/home/devel/public_html/domain.com/public/images/slideshow/');
define('WEBIMGDIR', '/images/slideshow/');
define('SS_SESSNAME', 'slideshow_sess');

$err = '';
session_name(SS_SESSNAME);
session_start();

$ss = new Slideshow($err);
if (($err = $ss->init()) !== '') {
    header('HTTP/1.1 500 Internal Server Error');
    echo $err;
    exit;
}

$ss->get_images();
list($curr, $caption, $first, $prev, $next, $last) = $ss->run();

class Slideshow {
    private $files_arr = null;
    private $err = null;

    public function __construct(&$err) {
        $this->files_arr = array();
        $this->err = &$err;
    }

    public function init() {
        if (!$this->dir_exists()) {
            return 'Error retrieving images, missing directory';
        }
        return '';
    }

    public function get_images() {
        if (isset($_SESSION['imgarr'])) {
            $this->files_arr = $_SESSION['imgarr'];
        } else {
            if ($dh = opendir(IMGDIR)) {
                while (false !== ($file = readdir($dh))) {
                    if (preg_match('/^.+\.(jpg|jpeg|gif|png)$/i', $file)) {
                        $this->files_arr[] = $file;
                    }
                }
                closedir($dh);
            }
            $_SESSION['imgarr'] = $this->files_arr;
        }
    }

    public function run() {
        $curr = 1;
        $last = count($this->files_arr);

        if (isset($_GET['img']) && preg_match('/^[0-9]+$/', $_GET['img'])) {
            $curr = (int)$_GET['img'];
            if ($curr <= 0 || $curr > $last) $curr = 1;
        }

        if ($curr <= 1) {
            $prev = $curr; $next = $curr + 1;
        } elseif ($curr >= $last) {
            $prev = $last - 1; $next = $last;
        } else {
            $prev = $curr - 1; $next = $curr + 1;
        }

        $caption = str_replace(['-', '_'], ' ', $this->files_arr[$curr - 1]);
        $caption = preg_replace('/\.(jpe?g|gif|png)$/i', '', $caption);
        $caption = ucfirst($caption);

        return array($this->files_arr[$curr - 1], $caption, 1, $prev, $next, $last);
    }

    private function dir_exists() {
        return file_exists(IMGDIR);
    }
}
?>

前端模板 (內嵌於同一檔案)

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Slideshow</title>
<style>
body{margin:0;padding:0;font:100% Verdana,Arial,sans-serif;font-size:14px;}
#gallery{border:1px #ccc solid;width:600px;margin:40px auto;text-align:center;}
#gallery img{margin:20px;border:2px #004694 solid;}
#gallery p{color:#004694;}
#gallery .pn{padding:10px;margin:0 5px;border-top:1px #ccc solid;}
a{color:#333;} a:hover{color:#cc0000;}
a.sp{padding-right:40px;}
</style>
</head>
<body>
<div id="gallery">
    <img src="<?=WEBIMGDIR?><?=$curr?>" alt="" />
    <p><?=$caption?></p>
    <div class="pn">
        <a href="?img=<?=$first?>">First</a> |
        <a href="?img=<?=$prev?>" class="sp">Previous</a>
        <a href="?img=<?=$next?>">Next</a> |
        <a href="?img=<?=$last?>">Last</a>
    </div>
</div>
</body>
</html>

部署步驟

  1. 建立圖片目錄並放入圖片檔案。
  2. 修改 IMGDIR (絕對路徑) 與 WEBIMGDIR (Web 可存取路徑)。
  3. 將上述程式碼存為 index.php 上傳至目標目錄。
  4. 瀏覽該 URL 即可看到輪播效果。

擴充方向

  • 加入自動播放 (JavaScript setInterval 切換 img 參數)。
  • 支援子目錄分類、EXIF 資訊顯示、鍵盤左右鍵導航。
  • 整合資料庫管理圖片標題、排序、標籤等中繼資料。
copyright © 萬盛學電腦網 all rights reserved