Today, there are many options to make screenshots of any URL with PHP. Let's examine them all and choose which suits you best.
How to take website screenshots with PHP
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:
They might overlap, but there is no best solution. Each depends on your use case and requirements.
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:
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.
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:
composer require nesk/puphpeteernpm 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.
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 support@screenshotone.com. And have a nice day 👋
By the way, you might also find interesting how to:
Interviews, tips, guides, industry best practices, and news.
Use Puppeteer or screenshot API to generate the Open Graph protocol images, bills, receipts, or invoices PDF or PNG files from the HTML templates.
Nowadays, you have various options to generate PDFs from HTML or any given URL: generating PDF in the browser, on the server-side (Node.js), or even using a modern and friendly API to generate PDF.
You don't need a screenshot API if it is a one-off task for you and you can use some library for screenshotting.
Exhaustive documentation, ready SDKs, no-code tools, and other automation to help you render website screenshots and outsource all the boring work related to that to us.