萬盛學電腦網

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

PHP 判斷 GIF 是否為動畫(多幀檢測)

核心原理

GIF 動畫本質為多幀影像疊加,文件內部包含 Netscape 2.0 應用擴展塊(Application Extension Block),標識為十六進位序列:

0x21 0xFF 0x0B 'NETSCAPE2.0'

單幀靜態 GIF 不含此塊。透過檢測該特徵序列即可判斷是否為動畫。

實作函數

<?php
/**
 * 判斷 GIF 是否為動畫
 * @param string $filename GIF 檔案路徑
 * @return int 1=動畫 0=靜態/非GIF/讀取失敗
 */
function isAnimatedGif($filename)
{
    $fp = @fopen($filename, 'rb');
    if (!$fp) {
        return 0;
    }

    // 讀取完整文件內容(GIF 通常較小,可接受)
    $content = fread($fp, filesize($filename));
    fclose($fp);

    // 搜尋 Netscape 2.0 應用擴展塊特徵序列
    $signature = chr(0x21) . chr(0xFF) . chr(0x0B) . 'NETSCAPE2.0';
    return strpos($content, $signature) !== false ? 1 : 0;
}

// 使用範例
var_dump(isAnimatedGif('animation.gif')); // int(1)
var_dump(isAnimatedGif('static.gif'));    // int(0)

進階版:解析幀數與延遲資訊

<?php
function getGifFrameInfo($filename)
{
    $fp = @fopen($filename, 'rb');
    if (!$fp) return false;

    $header = fread($fp, 6);
    if ($header !== 'GIF87a' && $header !== 'GIF89a') {
        fclose($fp);
        return false; // 非 GIF
    }

    fseek($fp, 6); // 跳過 Header (6) + Logical Screen Descriptor (7)
    fseek($fp, 13, SEEK_CUR);

    $frameCount = 0;
    $delays = [];

    while (!feof($fp)) {
        $byte = fread($fp, 1);
        if ($byte === '') break;

        $ord = ord($byte);
        if ($ord === 0x21) { // Extension Block
            $label = ord(fread($fp, 1));
            if ($label === 0xF9) { // Graphic Control Extension
                fseek($fp, 1, SEEK_CUR); // Block Size (1 byte)
                $packed = fread($fp, 1);
                $delayBytes = fread($fp, 2);
                $delay = ord($delayBytes[0]) | (ord($delayBytes[1]) << 8); // 1/100 秒
                $delays[] = $delay / 100;
                fseek($fp, 2, SEEK_CUR); // Disposal + Transparent Index + Terminator
            } elseif ($label === 0xFF) { // Application Extension (Netscape)
                $blockSize = ord(fread($fp, 1));
                fseek($fp, $blockSize, SEEK_CUR); // Skip
                while (($bs = ord(fread($fp, 1))) > 0) {
                    fseek($fp, $bs, SEEK_CUR);
                }
            } else {
                // Other extensions: skip sub-blocks
                while (($bs = ord(fread($fp, 1))) > 0) {
                    fseek($fp, $bs, SEEK_CUR);
                }
            }
        } elseif ($ord === 0x2C) { // Image Descriptor = 一幀
            $frameCount++;
            fseek($fp, 9, SEEK_CUR); // Skip Image Descriptor
            $packed = ord(fread($fp, 1));
            $lctSize = ($packed & 0x80) ? 3 * (1 << ($packed & 0x07)) : 0;
            fseek($fp, $lctSize, SEEK_CUR); // Skip Local Color Table
            $lzwMinCodeSize = ord(fread($fp, 1));
            // Skip image data sub-blocks
            while (($bs = ord(fread($fp, 1))) > 0) {
                fseek($fp, $bs, SEEK_CUR);
            }
        } elseif ($ord === 0x3B) { // Trailer
            break;
        }
    }
    fclose($fp);

    return [
        'frames' => $frameCount,
        'delays' => $delays,
        'is_animated' => $frameCount > 1,
        'total_duration' => array_sum($delays),
    ];
}

效能與替代方案對比

方法優點缺點
特徵序列搜尋極簡、快速、無依賴僅能判斷是否動畫,無法獲取幀數/延遲
GD imagecreatefromgif()PHP 內建、可獲取寬高僅讀取第一幀、無法判斷動畫
Imagick getNumberImages()完整解析幀數、延遲、尺寸需安裝 ImageMagick + php-imagick、較重
手動解析二進位零依賴、可獲取完整資訊代碼複雜、需處理邊界情況

建議選型

  • 僅需判斷是否動畫 → 特徵序列搜尋(本文方案)。
  • 需獲取幀數、延遲、尺寸 → Imagick(生產環境推薦)或 手動解析(無法安裝擴展時)。
  • 批量處理大量 GIF → 考慮 CLI 工具 gifsicle --info 配合 exec(),效能最優。
copyright © 萬盛學電腦網 all rights reserved