Examples
Retina screenshots with PHP and Puppeteer
Heads up before you start
Puppeteer has no official PHP port. The closest equivalent is chrome-php — an actively maintained PHP library that controls headless Chrome over the same DevTools Protocol Puppeteer uses. These examples use chrome-php.
Install chrome-php
composer require chrome-php/chrome chrome-php drives a Chrome or Chromium you already have installed — it does not download its own browser. It uses the CHROME_PATH environment variable if set, and otherwise looks in the standard install locations for your OS.
Take a retina screenshot and save to file
<?php
require __DIR__ . '/vendor/autoload.php';
use HeadlessChromium\BrowserFactory;
$browser = (new BrowserFactory())->createBrowser();
$page = $browser->createPage();
$page->setDeviceMetricsOverride([
'width' => 480,
'height' => 240,
'deviceScaleFactor' => 2,
'mobile' => false,
])->await();
$page->navigate('https://www.htmltoimage.com/demo')->waitForNavigation();
$page->screenshot()->saveToFile('retina.png');
$browser->close(); Find out how to take a full-page screenshot with PHP and Puppeteer.
That was 19 lines of code — plus a browser to run in production
Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:
curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com" Try the Urlbox screenshot API — free trial, no browser babysitting.