透過 COM 元件控制 IE 瀏覽器導航至指定網址,擷取視窗畫面並縮放儲存為 PNG。
// 擷取視窗 (IE)
$ie = new COM('InternetExplorer.Application');
$ie->Navigate2($webaddress);
$oWSH = new COM('WScript.Shell');
while ($ie->ReadyState != 4) usleep(10000);
$handle = $ie->HWND;
$ie->Visible = true;
while ($ie->Busy) com_message_pump(4000);
$im = imagegrabwindow($handle, 0);
$ie->Quit();
$file = 'public/images/' . time() . 'iesnap.png';
imagepng($im, $file);
// 全螢幕模式擷取並縮放
$browser = new COM('InternetExplorer.Application');
$handle = $browser->HWND;
$browser->Visible = true;
$browser->FullScreen = true;
$browser->Navigate($webaddress);
while ($browser->Busy) com_message_pump(4000);
$im = imagegrabwindow($handle, 0);
$browser->Quit();
$newImg = imagecreatetruecolor(206, 132);
$file = 'public/images/' . time() . 'ie.png';
imagecopyresampled($newImg, $im, 0, 0, 206, 132, 206, 142, 1024, 768);
imagepng($newImg, $file);
imagedestroy($newImg);
new COM('InternetExplorer.Application'):建立 IE 實例(需 Windows 環境、PHP 啟用 com_dotnet 擴充)。Navigate2() / Navigate():導航至網址。ReadyState == 4 / Busy:等待頁面載入完成。imagegrabwindow($handle, 0):擷取視窗畫面為 GD 影像資源。imagecreatetruecolor() + imagecopyresampled():建立真彩畫布並高品質縮放。imagepng():儲存為 PNG。更多 PHP 圖片處理技巧請參考本站專題:《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》。