Last week I
read about the functions imagegrabscreen() and imagegrabwindow(). They were added to
gdlib and are available in PHP since version
5.2.2. Because I got excited I installed the latest version of
WAMP5 on my windows XP laptop (the functions are windows-only) and started typing. The examples on
php.net however will not work at first when you use WAMP out-of-the-box, so this is a little how-to document about how to get it working.
1. Install WAMP and make sure gdlib is included
After installing WAMP and running the code from the examples, I received a message telling me that imagegrabscreen was an undefined function. It turned out that I had forgotten to uncomment the gdlib2 dll in php.ini. Note to myself: "if you want to use gdlib functions make sure that php actually uses gdlib"

. After uncommenting the line and restarting the WAMP services the functions were suddenly there.
2. Change service settings
Now that was the easy part. At this point, when I ran imagegrabscreen() I got a nice image returned. Good dimensions, no errors at all. The only problem now was that the image turned out to be entirely black! Good thing is that this problem is easy to google for ('imagegrabscreen black' should cover it) so I found the solution. I had to change my settings for the WAMP-service, allowing it to interact with the desktop. Now of course, this is because I am using WAMP. If you are using anything different you should edit a different service. However, if you get black images this is what you should do:
Go to the control panel, administrative tools, services. Find your webserver process (in my case wampapache, but can also be apache or iis or whatever) and open its properties. Now enable the "Allow service to interact with desktop" checkbox on the 'Log on' tab and press OK. After restarting WAMP, you shouldn't get black images anymore.
3. Memory limit
Last but not least, the imagegrabscreen and imagegrabwindow functions seem to be using quite some memory. Instead of getting an image I now got an exception telling me that I exceeded the memory limit. After changing the memory_limit directive in php.ini from 8MB to 80MB the function finally decided to give up and give me the screenshot I wanted.
Purpose
Basically imagegrabscreen does what your printscreen button does (except for copying it to the clipboard off course). The imagegrabwindow grabs a window in exactly the same way. Now the only purpose for these functions I could think of is 'to make a live screenshot of a website', so you could put little fancy thumbnails next to the external links on your website. That's about the only thing I can think of, but it seems that the functions were
created exactly for that purpose. However, because I wanted to prove this can be done with other applications than Internet Explorer as well I created a little piece of code making a screenshot of PhotoShop:
PHP:
<?php
$photoshop = new COM("Photoshop.Application");
$photoshop->Visible = true;
$photoshop->Open("C:\Documents and Settings\Harrie\My Documents\My Pictures\Sample Images\Flower\DSC_0001.JPG");
$im = imagegrabscreen();
$photoshop->Quit();
imagepng($im,"screenshot2.png");
?>
Result (scaled to fit page, obviously):
