PHP使用GD庫實現截屏php
PHP5.2.2以上版本的GD庫實現了兩個截屏函數 imagegrabscreen 和 imagegrabwindow
分別用於截取整個屏幕和截取某個窗口(同ALT+PrintScreen)的屏幕。web
1. 截取整個屏幕 Screenshotapache
<?php
$im
=
imagegrabscreen
()
;
imagepng
(
$im
,
"
myscreenshot.png
"
)
;
?>
2. 截取一個窗口 Capture a window (IE for example)瀏覽器
<?php
$browser
=
new
COM
(
"
InternetExplorer.Application
"
)
;
$handle
=
$browser
->
HWND
;
$browser
->
Visible
=
true
;
$im
=
imagegrabwindow
(
$handle
)
;
$browser
->
Quit
()
;
imagepng
(
$im
,
"
iesnap.png
"
)
;
$im
=
imagegrabscreen
()
;
?>
3. 截取IE內容 Capture a window (IE for example) but with its content!服務器
<?php
$browser
=
new
COM
(
"
InternetExplorer.Application
"
)
;
$handle
=
$browser
->
HWND
;
$browser
->
Visible
=
true
;
$browser
->
Navigate
(
"
http://www.21andy.com/blog/
"
)
;
/* Still working? */
while
(
$browser
->
Busy
)
{
com_message_pump
(
4000
)
;
}
$im
=
imagegrabwindow
(
$handle
,
0
)
;
$browser
->
Quit
()
;
imagepng
(
$im
,
"
iesnap.png
"
)
;
?>
4. 截取IE的全屏模式 IE in fullscreen mode函數
<?php
$browser
=
new
COM
(
"
InternetExplorer.Application
"
)
;
$handle
=
$browser
->
HWND
;
$browser
->
Visible
=
true
;
$browser
->
FullScreen
=
true
;
$browser
->
Navigate
(
"
http://www.21andy.com/blog/
"
)
;
/* Is it completely loaded? (be aware of frames!)*/
while
(
$browser
->
Busy
)
{
com_message_pump
(
4000
)
;
}
$im
=
imagegrabwindow
(
$handle
,
0
)
;
$browser
->
Quit
()
;
imagepng
(
$im
,
"
iesnap.png
"
)
;
?>
I use Internet Example Explorer as example, if you like to play more with IE and com, check out the IBrowser2 documentation at MSDN. It should work with any kind of window as long as you give the correct handle (usually $obj->HWND).測試
* php_gd2.dll for 5.2.x thread safe build
* php gd image documentation
* IE manual (useful to tweak it from com_dotnetui
在測試過程當中我並無出現手冊中說的那種效果,而是一張純黑的圖片,這是爲何呢?spa
可能有兩種狀況,第一種狀況就是這個COM組件只適用於WINDOWS服務器,由於他沒有IE瀏覽器;第二種狀況就是沒有打開容許服務與桌面交互!其中第二種狀況最爲常見(默認是關閉的),打開的方法:點擊計算機(個人電腦) -> 右鍵 -> 管理 -> 服務和應用程序 -> 服務 -> Apache -> 右鍵 -> 屬性 -> 登陸 -> 選中容許服務與桌面交互。.net
若是是第二種狀況的話,我安裝的是apache集成包,這樣的話就找不到apache的服務在哪裏,因此第二種方法的設置我沒有成功,若有成功者,但願指點一下。