How to take website screenshots with PHP
Posted June 14, 2022 by Dmytro Krasun ‐ 3 min read
Today, there are many options to make screenshots of any URL with PHP. Let's examine them all and choose which suits you best.
Today, there are many options to make screenshots of any URL with PHP:
- Selenium WebDriver client for PHP.
- You can use the PHP analog of Puppeteer.
- Or Screenshot API as a service.
They might overlap, but there is no best solution. Each depends on your use case and requirements.
Selenium WebDriver client for PHP
Selenium is a well-known kid in the QA automation area, so it is easy to start taking screenshots if you plan to write automation tests or already do it.
Install Selenium WebDriver client for PHP into your project:
$ composer require php-webdriver/webdriver
Download the latest version ChromeDriver, run it:
And you are ready to use it:
<?php require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\WebDriverBy; $serverUrl = 'http://localhost:4444'; $driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome()); $driver->get('https://example.com'); $driver->takeScreenshot('example.png'); $driver->quit();
If you just want to take one or two screenshots locally, the Selenium WebDriver client is not the best fit. As I wrote earlier, it better serves you already write automation tests with Selenium or plan to write them.
PHP analog of Puppeteer
While you can also use
PuPHPeteer for automation testing, it is much light weighter and simpler than Selenium and allows any automation over the browser. Everything you can do manually with a browser, you can also do with
And it is super easy to use:
composer require nesk/puphpeteer npm install @nesk/puphpeteer
And then just:
<?php require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use Nesk\Puphpeteer\Puppeteer; $puppeteer = new Puppeteer; $browser = $puppeteer->launch(); $page = $browser->newPage(); $page->goto('https://example.com'); $page->screenshot(['path' => 'example.png']); $browser->close();
It is very lightweight compared to Selenium and allows a broad spectrum for automation of the browser. You can use it for crawling, scrapping, taking screenshots, etc.
If you need the simplest way to take screenshots, you do not expect to take millions of them, and I would go with
PuPHPeteer. But if you want to take screenshots from different browsers or think about managing instances of browsers, there is a more straightforward way to go.
Screenshot API as a service
We specialize in taking screenshots and managing browser instances at scale. We provide a high-quality PHP client to take screenshots and cover a variety of use cases.
Easy to install:
composer require screenshotone/sdk:^1.0
And easy to use:
<?php use ScreenshotOne\Sdk\Client; use ScreenshotOne\Sdk\TakeOptions; $client = new Client('<access key>', '<secret key>'); $options = TakeOptions::url("https://example.com") ->fullPage(true) ->delay(2) ->geolocationLatitude(48.857648) ->geolocationLongitude(2.294677) ->geolocationAccuracy(50); $url = $client->generateTakeUrl($options); echo $url.PHP_EOL; // expected output: https://api.screenshotone.com/take?url=https%3A%2F%2Fexample.com... $image = $client->take($options); file_put_contents('example.png', $image); // the screenshot is stored in the example.png file
If you feel that it is the best fit for you, feel free to sign up for our screenshot API and get the access key.
Pick the solution which suits your needs best. If you decide to go with our API, please ask any questions and mail us at email@example.com. And have a nice day 👋
By the way, you might also find interesting how to: