Screen capture with PHP and GD
Posted by Pierre in
Uncategorized
Tuesday, April 17. 2007
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
).
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 to Edin Kadribaši?, you can fetch a 5.2.x (Threadsage build only) DLL here, simply replace the php_gd2.dll by this one (it is what will be in 5.2.2).
- Screenshot
<?php $im = imagegrabscreen(); imagepng($im,“myscreenshot.png”); ?>
- Capture a window (IE for example)
<br /> <?php <br /> $browser = new <acronym title="“InternetExplorer.Application”">COM</acronym>;<br /> $handle = $browser->HWND;<br /> $browser->Visible = true;<br /> $im = imagegrabwindow($handle);<br /> $browser->Quit(); imagepng($im, “iesnap.png”);<br /> $im = imagegrabscreen(); <br /> ?><br />
- Capture a window (IE for example) but with its content!
<?php $browser = new <acronym title="“InternetExplorer.Application”">COM</acronym>; $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 <acronym title="“InternetExplorer.Application”">COM</acronym>; $handle = $browser->HWND; $browser->Visible = true; $browser->FullScreen = 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”); ?>
I use Internet 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_dotnet
Markus - #2 - 2007-04-18 08:10 - (Reply)
Any examples of doing this with FF too? Does FF provide COM objects?
Pierre - #4 - 2007-04-18 09:56 - (Reply)
Byron:&quot;Thanks Pierre! I've always wondered how to go about this....&quot; Same here 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?
Andrew - #5 - 2007-04-18 17:30 - (Reply)
@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
Pierre - #6 - 2007-04-18 18:45 - (Reply)
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.
Marcel - #7 - 2007-04-19 08:44 - (Reply)
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
Lolo Irie - #8 - 2007-04-19 09:59 - (Reply)
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 ?
Lolo Irie - #9 - 2007-04-19 10:14 - (Reply)
Autant pour moi, le post de Andrew resout le problème.
FORMIDABLE !!! :p Thanks Andrew, your answer solved my problem !
bill911 - #10 - 2007-04-19 15:06 - (Reply)
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/
Hamish M - #11 - 2007-04-19 15:58 - (Reply)
Great idea! Though I think you mean "Internet Explorer", when you said "I use Internet Example as example"
Pierre - #12 - 2007-04-19 16:41 - (Reply)
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.
jca0811 - #13 - 2007-04-20 12:21 - (Reply)
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 ?
Pierre - #14 - 2007-04-20 13:19 - (Reply)
@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 ![]()
Marcel - #15 - 2007-04-20 23:39 - (Reply)
Anyone care to answer a least part of my (obvious) stupid questions? See—>comment 7 Many thanks.
Pierre - #16 - 2007-04-21 00:09 - (Reply)
"+ 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.
tony - #18 - 2007-04-27 22:14 - (Reply)
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?
Pierre - #21 - 2007-05-13 09:55 - (Reply)
Hi Silvano! IRC on freenode or per email are two ways to reach me ![]()
Mat - #22 - 2007-05-14 13:19 - (Reply)
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?
Mat - #23 - 2007-05-14 13:40 - (Reply)
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?
Pierre - #24 - 2007-05-16 12:23 - (Reply)
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.
Rob - #26 - 2008-03-27 19:50 - (Reply)
I love this idea, I was able to get it working in no time.
I don’t think this is possible with PHP (since its a server side language), but I’d like to have a screen capture of the client side. For example: a "report bug" feature on a website, when the user clicks it, it sends a screen shot along with other relevant text information to an email.
I doubt its possible in PHP, but is there other solutions or am I asking the impossible?
Pierre - #26.1 - 2008-03-27 20:48 - (Reply)
It is possible to do it using PHP on the server side. You only have to give PHP (via IIS or the server you use) the desktop permissions. See the PHP manuals, there is some comments about that, how to do it, etc.
It is actually why I initially implemented these functions, to automate tests and output snapshots of screens/pages when results differ.
About your question, It could be possible to use the same function inside a little desklet application which takes a snaphost and ask the user to enter some comment (or automatically filled), in PHP or c#.
Rob - #26.1.1 - 2008-03-27 21:07 - (Reply)
I think I miscommunicated a little, I implemented it on a page (on my webserver) and opened the php file in the browser of another system, but the screenshot it drops is of the server. I got it working, and will probably use this server side screen capturing in other apps, but at the moment I’m looking for a way to get the client’s screenshot without them having to do anything (users get confused easily
)
How would I go about creating a desklet application? How is it different then the standard php files I run from my server? I’d like to learn more about this sort of functionality, do you have any resources I could read through?
Pierre - #26.1.1.1 - 2008-03-31 17:57 - (Reply)
In theory it is not very different. In practice it will require to install PHP on your clients windows, which is not a small thing.
It should be possible to use one of this PHP compiler (I never tried them so….). But it is certainly easier to do it in C++ or C#, that’s what I use for windows desktop developments. Which features do you need besides the screen shot itself?
Rob - #27 - 2008-03-31 18:13 - (Reply)
well ideally it would be nice to have the screenshot taken and automatically emailed to a pre-described email address. I found a nice firefox add-on called fireshot, but it uses the desktops outlook to send the email, which wont work since we use novell groupwise. (this also wouldn’t work since it would need to be cross platform, and work in in IE as well)
However, the image could also be ftp’d to a server.
Pierre - #27.1 - 2008-04-01 10:37 - (Reply)
Are you looking for a tool to take custom screenshots (screen or custom application) or only from a browser window?
If yes, I don’t see an easy way to do it for browser only without writing a plugin for firefox and one for IE (activex for example), both can use the same underlying library and configuration.
If not, you will need a small application running in background and called on demand (taskbar, short keys, etc.).
Rob - #27.1.1 - 2008-04-01 15:04 - (Reply)
I really only need the browser window. But I don’t think I’ll be able to do this without more work then its worth, I’ll just stick with the old trusty ‘Print Screen’ button. Thanks for all the help Pierre, your work on these pages is inspiring ![]()
Felipe Benavente - #28 - 2008-04-01 23:59 - (Reply)
Hi Pierre, I´m trying to use imagegrabwindow to do the following:
I have a website where the users can see some charts and what I want is that these users can take a picture of his favourite chart (here I want to use imagegrabwindow) and send it by email. I dont know how can I get the window´s handle of the window where the chart appears. In your examples you made it with a new IE window, and I want to use the same window where the chart is, and that means any browser window.
Rob - #28.1 - 2008-04-02 14:45 - (Reply)
Felipe, I was able to do this, but it wasnt what I needed so I ended up abandoning it, but this is how I accomplished it.
Pass the page location, along with any post/get requests to the server, on the server, you open a browser window and point it to the page that was sent from the client, use the imagegrab, and dump it to a file on the webserver, then use something like php mailer (http://phpmailer.codeworxtech.com/) to send the email with the image as an attachment.
Hope this helps (and that its accurate).
Priya - #29 - 2008-04-21 10:24 - (Reply)
Hai Pierre, I am impressed with your work on these pages.
I need the php code that captures the screen as an image file for the user given website address.
Can you help me out.
Pierre - #29.1 - 2008-04-21 10:43 - (Reply)
Hi Priya,
You can find an example in the PHP manual:
http://www.php.net/imagegrabwindow
vinay - #30 - 2008-05-13 08:42 - (Reply)
This is really excellent, but when i am trying it generates only blank (black) image.
Please help me. Thanks
Pierre - #30.1 - 2008-05-13 10:34 - (Reply)
Have you added the user running your webserver to the desktop group?
See the notes here:
http://www.php.net/manual/en/function.imagegrabscreen.php
Sascha - #31 - 2008-05-14 16:17 - (Reply)
Hi Pierre,
imagegrabscreen() is a great addon to PHP, but somehow I can’t use it with my current webserver configuration and that’s why I would like to ask for your support:
I first created a threeliner to grab a screenshot and save it to disk; I called it directly via cli and it worked like a charm.
Being convinced that the script would also work from within my webserver, I altered one of my monitoring scripts to grab the screen, save it and show the image on a web page, but all I got was a white (not black like mentioned in other posts) image!
function os_show_screen (){ $im = imagegrabscreen(); unlink(‘temp/screen.png’); imagepng($im, ‘temp/screen.png’); print ‘&lt;img src=\‘temp/screen.png?’.time().’\’&gt;";<br /> }
I checked that both IIS and the WWW Publishingservice were allowed to interact with the desktop, but that didn’t help.
My current setup is as follows:
Windows 2000 Server, IIS 5 with ZendCore for Oracle running PHP as FastCGI
Do you have a clue what would cause this behaviour?
Thanks a lot (also for the great contribution)!!
P.S. The code is a bit messed up but I hope you’ll still see how the script works.
P.P.S. The code highlighter should end with [/geshi] instead of [/lang] ![]()
Pierre - #31.1 - 2008-05-15 11:47 - (Reply)
I’m not sure what’s wrong sorry. It works on all our boxes here and by dozen users out there.
Can you try using grabwindow with IE or any other app?
Sascha - #31.1.1 - 2008-05-16 09:04 - (Reply)
Hi Pierre,
imagegrabwindow() doesn’t work on my local computer and on the remote server, although I don’t get any error messages at all from my local test system.
What’s even stranger: imagegrabscreen() works correctly on my local system, but it still creates a blank image on the remote development platfrom. When I use imagegrabscreen() from a shell php script on the remote machine, the screenshot is correctly created and saved. I am absolutely clueless why my local and remote system behave differently …
Sascha - #31.1.2 - 2008-05-16 09:51 - (Reply)
Additional comment:
I was wrong when I said that the CLI script call created a screenshot – I copied my local files to the remote machine, called the script and the file seemed to be created; instead it hadn’t been overwritten because of access restrictions … as a result, imagegrabscreen() never worked on my windows 2000 server ![]()
Pierre - #31.1.2.1 - 2008-05-16 15:55 - (Reply)
I can’t say what’s wrong on your remote host without seeing its configuration, versions or SP.
Feel free to send more details if you still need a hand for this problem
Paul Derbyshire - #32 - 2008-06-08 12:01 - (Reply)
I have apache running on Vista, set the service allowed to interact with desktop. It creates the com object for IE, but it pops up with interactive services popup msg.
Apparently it runs it as a system process which is seperated from desktop. I do get the image of the browser saved, but I’d actually like to keep the window open to do some more stuff with it, but alas, can’t access that window.
Also imagegrabscreen() still just gives a black image.
DSS_Creative - #33 - 2008-07-09 22:45 - (Reply)
If imagegrabscreen() still just gives a black image (and you already allowed Apache to interact with the desktop)... You may have to Login to the server so it’s just sitting at the desktop. Then you can run the PHP page from a client machine.
Colonel32 - #33.3 - 2008-08-06 05:56 - (Reply)
i’m still getting a black screen so i’m looking for any other tips or ideas.
i’m on a dell running vista, apache 2.2, and PHP Version 5.2.6. phpinfo says GD is enabled and functional and i am getting a black image.
i’ve changed my Apache service to interact with the desktop and i’ve also tried to use the optional "This Account" with differnt user but still to no avail.
i’m just hitting a script that has the below code:
$im = imagegrabscreen();
imagepng($im,"C:\screenshot2.png");
any help would be great. thx. c32
vinay - #33.3.1 - 2008-08-14 07:40 - (Reply)
I worked on image capture using PHP GD.
It is working successfully.
If getting black screen then you need to enable services to make Apache to interact from desktop.
From my computer-> manage->services->Apache-> Allow to interact with desktop
Now u will get captured image.
Thanks
pepcomix - #35 - 2008-11-01 07:46 - (Reply)
Hi Pierre, I’m trying to get screenshot by using imagegrabwindow.
I’m testing on Windows 2003 server with Apache2.2 and it’s also allowed to interact with the desktop, but I’ve got weird black image like below.
http://pepcomix.net/tmp/test3.jpeg
Window frame can be rendered, but the content is in black.
Have a go at logon remote desktop, then run the script with command line PHP, it’s rendered correctly.
I’m using below code.
"Capture a window (IE for example) but with its content"
http://jp2.php.net/manual/en/function.imagegrabwindow.php
Could you explain whtats wrong with this?
Thank you.


