This really isn’t a tutorial and well the code could surely use some improvements, but I just wanted to post how I solved a certain problem earlier. The problem was that I had a list of 300+ URLs and I needed screenshots of them all. Read on to see how I did it.
There are a few things that one should keep in mind first. This only works on a Windows server. I can’t stress this enough as the function that actually grabs the screenshot only works under Windows. I’m still looking for an easy linux server based solution but so far nothing.
Secondly this was done on my work PC, which runs at 1920×1200. Why is this important? Because it’s in the code as well and you’ll have to adjust that. In the code it’s 1920 and 1180. 1180 because that’s the 20 pixels I decided to remove from the bottom where the IE status bar is.
Yes, it’s probably not 20 pixels tall, but 1180 is tall enough without it really making any difference. And yes, I do know that you can turn the status bar off, but when I did that all my screenshots would be just blank black images.
I believe that’s all really, so here’s the code. Have fun with it.
# I found that setting this to basically 10*(number of webpages) did the trick
set_time_limit(3200);
# The file with each URL on a new line
$url = file('urls.txt');
# Open Internet Explorer
$browser = new COM('InternetExplorer.Application');
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Fullscreen = true;
# Loop through the array of URLs
foreach ($url as $key => $open)
{
# Open the URL
$browser->Navigate($open);
# Wait for the page to finish loading
while ($browser->Busy) {
com_message_pump(4000);
}
# Grab the screenshot
$im = imagegrabwindow($handle, 0);
# Create a new image and copy the screenshot minus the bottom status bar from IE
$dest = imagecreatetruecolor(1920, 1180);
imagecopy($dest, $im, 0, 0, 0, 0, 1920, 1180);
# I decided to name my screenshots after the domain (eg. www.test.com.png)
$domain = parse_url($open);
imagepng($dest, 'screenshots/' . $domain['host'] . '.png');
imagedestroy($im);
imagedestroy($dest);
}
# Close IE
$browser->Quit();
?>
If anyone knows of something as simple as this for linux, I’d love to know about it. But since I didn’t need these screenshots to be created on the fly this did the trick.