IECapt 是一款基於 Internet Explorer 內核的命令列截圖工具,支援將網頁輸出為 BMP、JPEG、PNG 格式。提供 C++ 與 C# 兩個版本,但強依賴 IE 環境,僅能在 Windows 系統運行。若伺服器為 Linux,請改用 headless Chrome (Puppeteer) 或 wkhtmltoimage。
IECapt.exe 放置於 PHP 腳本同目錄或指定路徑。<?php
header('Content-Type: text/html; charset=utf-8');
function capturePage($url, $saveDir) {
if (empty($saveDir) || !is_dir($saveDir)) {
if (!@mkdir($saveDir, 0755, true)) {
exit('截圖保存目錄建立失敗!');
}
}
// 簡易 URL 可達性檢查
$headers = @get_headers($url);
if (!$headers || strpos($headers[0], '200') === false) {
exit('目標 URL 無法訪問或不存在!');
}
$fileName = rtrim($saveDir, '/') . '/' . time() . '.png';
$iecaptPath = __DIR__ . '/IECapt.exe';
$cmd = sprintf('"%s" --url=%s --out="%s"', $iecaptPath, escapeshellarg($url), $fileName);
system($cmd, $ret);
if ($ret !== 0) {
error_log("IECapt 執行失敗: $cmd");
return false;
}
return $fileName;
}
// 使用範例:注意必須包含 www,否則 IE 可能返回空白頁
$img = capturePage('https://www.csdn.net/', __DIR__ . '/img');
echo $img ? "截圖成功:$img" : '截圖失敗';
?>
www 或完整域名;目標站若有反爬蟲/JS 渲染,IE 可能無法正確顯示。--delay=5000(毫秒)或使用 proc_open 實現非阻塞超時控制。| 工具 | 跨平台 | 渲染引擎 | 適合場景 |
|---|---|---|---|
| IECapt | ❌ 僅 Windows | IE (Trident) | 舊專案、需 IE 相容性測試 |
| Puppeteer (Chrome Headless) | ✅ 全平台 | Blink | 現代 Web、SPA、複雜 JS |
| wkhtmltoimage | ✅ 全平台 | WebKit | 靜態頁、PDF 生成 |
新專案強烈建議直接上 Puppeteer + Node.js 微服務,PHP 僅負責調用 API,避開 Windows/IE 的維護陷阱。