With about 80% of the world’s websites running on PHP as a server-side language, PHP is alive and well. So if you find yourself needing to take website screenshots, we’ve got you!
Today, we’ll show you how to take website screenshots with PHP.
Before we start, let’s look at your toolkit for this tutorial. If you want to take PHP screenshots, you’ll need:
● A web server, either hosted with a hosting provider or installed locally on your computer using something like Apache
● PHP installed on the server
Once these are installed and configured, you can start writing the scripts to take website screenshots.
Like many other programming languages, PHP has a few built-in functions that let you take screenshots.
The first, imagegrabscreen, is probably the simplest. It takes a screenshot of the user’s entire screen and exports it as a .png file.
You’ll need the following code to use it:
<?php
$im = imagegrabscreen();
imagepng($im, "myscreenshot.png");
imagedestroy($im);
?>
When executed, this function will take a screenshot of the user’s current screen and save it as a png file.
The other option is using imagegrabwindow, which lets you take a screenshot of a specific window and its content. Here, you’ll use the following code:
<?php
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Fullscreen = true;
$browser->Navigate("http://www.google.com");
/* Still working? */
while ($browser->Busy) {
com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagepng($im, 'iesnap.png');
imagedestroy($im);
?>
When executed, this function will take a screenshot of the window and save it as a .png file.
Now, if this scratches your screenshot itch - great! You’re done. But if you’re sensing some limitations to the way we take screenshots in PHP, you’re not the only one.
For example, the imagegrabscreen function only takes a screenshot of the user’s primary display. This means that in the case of a user using multiple displays, the primary display should have the browser window open.
Also, these functions don’t provide a way to take website screenshots in bulk.
And if your OS of choice isn’t Windows, too bad. These functions are only available on Windows systems. If you want to take website screenshots on Linux and Mac systems, you’ll need another solution like using an external library or writing your own script.
Either way, these solutions might require some tinkering on your part to get them right.
Luckily, there is an easier way. You can use ScreenshotAPI to take screenshots programmatically quickly and easily in various formats and resolutions.
To use the API, you’ll first declare the variables that you’ll use to construct your API query parameters in your PHP tags.
These variables include:
<?php
$token = "Your API Key";
$url = urlencode("https://google.com");
$width = 1920;
$height = 1080;
$output = "image";
After you’ve defined the variables, you can construct your query parameters:
$query = "https://shot.screenshotapi.net/screenshot";
$query .= "?token=$token&url=$url&width=$width&height=$height&output=$output";
You can then call the API and save the screenshot:
$image = file_get_contents($query);
file_put_contents("./screenshot.png", $image);
?>
Now, the last method, as you’ve probably seen, is a lot simpler. And once you’ve written the script, it also makes it possible to take screenshots in bulk, either of a single website or several.
If you’re tired of doing everything manually, get your API key at ScreenshotAPI!