Examples
HTML to an in-memory image buffer 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.
Get the image as an in-memory buffer
<?php
require __DIR__ . '/vendor/autoload.php';
use HeadlessChromium\BrowserFactory;
$browser = (new BrowserFactory())->createBrowser();
$page = $browser->createPage();
$page->setHtml('<html><body><h1>Hello World</h1></body></html>');
$image = $page->screenshot()->getRawBinary(); // binary string, not written to disk
printf("got %d bytes\n", strlen($image));
$browser->close(); Find out how to convert HTML to a base64 image with PHP and Puppeteer.
That was 14 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.