Screen capture with PHP and GD

来源:百度文库 编辑:神马文学网 时间:2024/04/28 05:44:59
(注:下面的只是以IE为例,不是只能调IE的,只要有调用的句柄就能用)
Screen capture with PHP and GD
http://blog.thepimp.net/index.php/post/2007/04/17/Screen-capture-with-PHP-and-GD
By Pierre on Tuesday, April 17 2007, 18:04 -Permalink
COMdotNetGDIEInternet ExplorerlibGDPHPscreenshotwindowswindows capture
To get a snapshot of a HTML page, a window or a complete screen was always something tricky to do in PHP. For one of my current projects, I had to check that our changes did not affect visually any page. An easy way to achieve this goal is to compare the rendered pages in the browsers itself, easy and time consuming (for a human being :D).
That‘s why I finally sit down and implemented imagegrabscreen and imagegrabwindow . They capture respectively the whole screen or a window (using its handle).
Thanks toEdin Kadribaši?, you can fetch a 5.2.x (Threadsage build only) DLLhere, simply replace the php_gd2.dll by this one (it is what will be in 5.2.2).
Screenshot
$im = imagegrabscreen();
imagepng($im, "myscreenshot.png");
?>
Capture a window (IE for example)
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$im = imagegrabwindow($handle);
$browser->Quit();
imagepng($im, "iesnap.png");
$im = imagegrabscreen();
?>
Capture a window (IE for example) but with its content!
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://blog.thepimp.net");
/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>
IE in fullscreen mode
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->FullScreen = true;
$browser->Navigate("http://blog.thepimp.net");
/* 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 atMSDN. 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 buildphp gd image documentationIE manual (useful to tweak it from com_dotnet

Comments
1. On Tuesday, April 17 2007, 23:47 by Byron
Thanks Pierre! I‘ve always wondered how to go about this....
2. On Wednesday, April 18 2007, 08:10 byMarkus
Any examples of doing this with FF too? Does FF provide COM objects?
3. On Wednesday, April 18 2007, 09:02 by Marco
I just get black png files. Whats wrong?
4. On Wednesday, April 18 2007, 09:56 byPierre
@Byron:"Thanks Pierre! I‘ve always wondered how to go about this...."
Same here :D
@Markus:"Any examples of doing this with FF too? Does FF provide COM object"
No idea. If not it is maybe possible using some custom .net code. Please leave a comment if you find a solution
@Marco "I just get black png files. Whats wrong?"
Code?
5. On Wednesday, April 18 2007, 17:30 by Andrew
@Marco:
If you are running windows xp here is what I did:
The service that your webserver runs under needs to be able to interact with the desktop to take screen caps. I had this same problem. Right click my computer-> manage-> services find the service for your webserver if it logs in as a system account just check the "Interact with desktop" check box. Otherwise I think you need to use the local group policy editor.
HTH
6. On Wednesday, April 18 2007, 18:45 byPierre
If someone needs Mozilla (aka firefox too) tests, I think the Mozilla Controls can be useful:
http://www.iol.ie/~locka/mozilla/mo...
I‘m not sure if it is possible to create a control only using com_dotnet. Feel free to post the scripts here, I can then update the post with a mozilla alternative.
7. On Thursday, April 19 2007, 08:44 byMarcel
Hi Pierre,
Being quite a newbie in this, I am trying to figure out how this will all work.
Let‘s say that I want to grap a screen of my favorite newsite each 15 minutes or so:
+ would some php code on my externally hosted server activate my PC (winXP Home) at home to do the screenshot and return the image captured to the server?
+ or can it function without a PC running?
If I assume a running pc is necessary, would you notice the script in action while working on the PC?
Sorry for the stupid questions...:)
Thanks
8. On Thursday, April 19 2007, 09:59 byLolo Irie
Bonjour et bravo,
Cela semble extremement prometteur, mais moi aussi avec PHP 5.2.0 sous Win XP, et avec IE6 je ne vois qu‘une image noire avec le deuxieme code de la page ici.
Une idée ?
9. On Thursday, April 19 2007, 10:14 byLolo Irie
Autant pour moi, le post de Andrew resout le problème.
FORMIDABLE !!! :p
Thanks Andrew, your answer solved my problem !
10. On Thursday, April 19 2007, 15:06 by bill911
There is a commercial product that I have used for years that is much more versatile and robust. The product is called snag it. It is very cheap. their website is:http://www.techsmith.com/
11. On Thursday, April 19 2007, 15:58 by Hamish M
Great idea!
Though I think you mean "Internet Explorer", when you said "I use Internet Example as example"
12. On Thursday, April 19 2007, 16:41 byPierre
@Bill911
In what this product (49$ per user) has anything to do with automatic screen captures from PHP? It is even not possible to use from a script.
Is it me or this comment smells like an Ad?
@Hamish
Thanks, fixed.
13. On Friday, April 20 2007, 12:21 by jca0811
I tried it and it works fine!
Now, I need to get screenshots covering the whole page, not only the visible part. Do you know how to make the browser move down over the page before getting the screenshot ?
14. On Friday, April 20 2007, 13:19 byPierre
@jca0811
It should be possible to do it using the Document property. See:
http://msdn.microsoft.com/workshop/...
Let us know how it works out
15. On Friday, April 20 2007, 23:39 byMarcel
Anyone care to answer a least part of my (obvious) stupid questions?
See-->comment 7
Many thanks.
16. On Saturday, April 21 2007, 00:09 byPierre
"+ would some php code on my externally hosted server activate my PC (winXP Home) at home to do the screenshot and return the image captured to the server?"
Yes and no. Yes, as it is possible to remotely activate a PC and no as I don‘t think it is a good idea.
"+ or can it function without a PC running?"
What I would suggest is to run the script on your PC at home and make it store the images on your server.
17. On Thursday, April 26 2007, 12:39 by msaraujo
nice and curious feature
Damned Firefox!
18. On Friday, April 27 2007, 22:14 by tony
Hi, This is off topic, but I can‘t find out anywhere how to set the compression level on the pecl zip
How do I use CM_STORE?
19. On Thursday, May 10 2007, 15:14 by devil
cool keep it up
20. On Saturday, May 12 2007, 18:42 bySilvano
Pierre, how can I reach you my friend? Gotta talk to you
21. On Sunday, May 13 2007, 09:55 byPierre
Hi Silvano!
IRC on freenode or per email are two ways to reach me
22. On Monday, May 14 2007, 13:19 by Mat
Any examples on how you go about using this code?
All i get if i save the code to a new .php page is :
Call to undefined function imagegrabscreen()
Some examples would be nice, it seems some of the code is missing - where is imagegrabscreen?
23. On Monday, May 14 2007, 13:40 by Mat
ok i see now you have to use a modified version of the gd library. Is there any way of using your functions without having to use your modified gd library?
24. On Wednesday, May 16 2007, 12:23 byPierre
Hi Mat,
"ok i see now you have to use a modified version of the gd library. Is there any way of using your functions without having to use your modified gd library?"
You need php 5.2.2 or later or use the dll provided here.