How to take website screenshots with PHP

Published on 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:

  1. Selenium WebDriver client for PHP.
  2. You can use the PHP analog of Puppeteer.
  3. 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:

Terminal window
$ composer require php-webdriver/webdriver

Download the latest version ChromeDriver, run it:

Terminal window
chromedriver --port=4444

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

PuPHPeteer is the closest analog to Node.js Puppeteer in PHP, a faster, more straightforward way to drive browsers supporting the Chrome DevTools Protocol in PHP.

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 PuPHPeteer.

And it is super easy to use:

Terminal window
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:

Terminal window
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.

Summary

Pick the solution which suits your needs best. If you decide to go with our API, please ask any questions and mail us at support@screenshotone.com. And have a nice day 👋

By the way, you might also find interesting how to: