Php如何模擬網頁浏覽器生成網站的縮略圖
<?php
/*
1.隻能工作在windows下麵,
2.web 服務器(iis或apache)做爲windows服務時,必須打開"允許與桌麵交互"的選項.(點擊服務屬性->豋錄->勾選"允許與桌麵交互",否則會出現黑屏
3.需要gb庫版本爲2.0.34(php5.2.2默認自帶)
4.linux下可以使用khtml2png插件,網址: http://khtml2png.sourceforge.net/index.php?page=download
*/
set_time_limit(60);
//截屏
$im = imagegrabscreen();
imagepng($im, "snap1.png");
//抓取IE窗口
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$im = imagegrabwindow($handle);
$browser->Quit();
imagepng($im, "snap2.png");
$im = imagegrabscreen();
//抓取IE窗口及窗口内容(IE爲例)
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate(" http://www.mulujidi.com");
/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "snap3.png");
// IE全屏模式
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->FullScreen = true;
$browser->Navigate(" http://www.mulujidi.com");
/* Is it completely loaded? (be aware of frames!)*/
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "snap4.png");
//生成網站縮略圖
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Fullscreen = true;
$browser->Navigate(" http://www.mulujidi.com");
while ($browser->Busy) {
com_message_pump(4000); //等待4秒
}
$im = imagegrabwindow($handle, 0); //抓取網頁圖像,需要php5.2.2以上版本的支持
$browser->Quit();
$new_img = imagecreatetruecolor(200, 150);
imagecopyresampled($new_img, $im, 0, 0, 0, 0, 200, 150, 1024, 768);
imagejpeg($new_img, 'snap5.jpg', 100);
imagedestroy($new_img);
echo "Done!";
?>