PHPExcel (已停維,現建議遷移至 PhpSpreadsheet) 可讀取 Excel 儲存格中的嵌入圖片。本文示範如何遍歷工作表繪圖集合,提取圖片資料流並存檔。
composer require phpoffice/phpspreadsheet
# 舊專案仍用 PHPExcel 可用 phpoffice/phpexcel (已棄用)
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
$inputFile = "goods_list.xlsx"; // 或 .xls
$outputDir = "extracted_images/";
if (!is_dir($outputDir)) mkdir($outputDir, 0755, true);
$spreadsheet = IOFactory::load($inputFile);
$sheet = $spreadsheet->getActiveSheet(); // 或 getSheetByName('Sheet1')
foreach ($sheet->getDrawingCollection() as $drawing) {
// 1. 取得圖片所在儲存格座標 (如 'A1')
$coordinate = $drawing->getCoordinates();
// 2. 取得檔名 (若無則自動生成)
$filename = $drawing->getIndexedFilename(); // 可能為空
if (!$filename) {
$ext = $drawing->getExtension(); // 依圖片類型自動判斷副檔名
$filename = "img_{$coordinate}.{$ext}";
}
// 3. 取得圖片二進位內容
$imageContent = null;
if ($drawing instanceof MemoryDrawing) {
// 記憶體圖片 (通常 .xlsx)
ob_start();
call_user_func($drawing->getRenderingFunction(), $drawing->getImageResource());
$imageContent = ob_get_clean();
} elseif ($drawing instanceof Drawing) {
// 檔案連結圖片 (通常 .xls 或外部連結)
$path = $drawing->getPath();
if (filter_var($path, FILTER_VALIDATE_URL)) {
// 遠端圖片需下載
$imageContent = file_get_contents($path);
} elseif (file_exists($path)) {
$imageContent = file_get_contents($path);
}
}
if ($imageContent) {
$savePath = $outputDir . $filename;
file_put_contents($savePath, $imageContent);
echo "已儲存: {$savePath} (來源儲存格: {$coordinate})" . PHP_EOL;
} else {
echo "無法讀取圖片內容: {$coordinate}" . PHP_EOL;
}
}
echo "處理完成。" . PHP_EOL;
| 類別 | 關鍵方法 | 用途 |
|---|---|---|
Drawing | getCoordinates(), getPath(), getExtension() | 儲存格座標、圖片路徑 (本地/URL)、副檔名 |
MemoryDrawing | getImageResource(), getRenderingFunction() | 取得 GD 資源與渲染函式 (imagepng/imagejpeg 等) |
Worksheet | getDrawingCollection() | 取得該工作表所有繪圖物件 (含圖片、圖表) |
MemoryDrawing 內嵌於 XML (xl/drawings/),需用 GD 渲染輸出。Drawing 指向內部儲存流或外部路徑,可直接 file_get_contents。圖片本身不屬於儲存格,但有「錨點」座標。若需將圖片與同列/同欄的資料關聯:
// 取得圖片錨點座標 (如 'B5')
$coordinate = $drawing->getCoordinates();
// 轉換為行列索引
$col = Coordinate::columnIndexFromString($coordinate[0]); // 'B' -> 2
$row = (int)substr($coordinate, 1); // 5
// 讀取同列其他欄位資料
$productId = $sheet->getCellByColumnAndRow(1, $row)->getValue(); // A 欄
$productName = $sheet->getCellByColumnAndRow(3, $row)->getValue(); // C 欄
// 以商品 ID 命名圖片
$filename = "product_{$productId}.{$ext}";
| 問題 | 原因與解決 |
|---|---|
| 記憶體耗盡 | 大量圖片請分批處理、或使用 ReadFilter 只讀繪圖、處理完 unset($drawing) 釋放 GD 資源。 |
| 圖片模糊/尺寸錯誤 | MemoryDrawing 為原始解析度,Excel 顯示時可能縮放。可讀取 getWidth()/getHeight() 並 imagescale() 調整。 |
| 透明背景變黑 | GD 預設輸出 JPEG 無透明。偵測 getExtension() === 'png' 時用 imagepng 輸出保留 Alpha 通道。 |
| 找不到圖片 | 確認 Excel 確實有「插入圖片」而非「連結圖片」(連結圖片路徑可能失效)。 |
// 命名空間對應
// PHPExcel_IOFactory → PhpOffice\PhpSpreadsheet\IOFactory
// PHPExcel_Worksheet_Drawing → PhpOffice\PhpSpreadsheet\Worksheet\Drawing
// PHPExcel_Worksheet_MemoryDrawing → PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing
// PHPExcel_Cell_Coordinate → PhpOffice\PhpSpreadsheet\Cell\Coordinate
// 其餘 API 幾乎相容,直接替換 namespace 即可運行。
使用 PhpSpreadsheet 遍歷 getDrawingCollection(),區分 MemoryDrawing (GD 渲染) 與 Drawing (檔案讀取) 兩種圖片來源,結合座標關聯業務資料,即可完整提取 Excel 內嵌圖片。建議新專案直接採用 PhpSpreadsheet,舊專案規劃遷移以獲安全更新與 PHP 8+ 相容性。