Examples
How to convert HTML to a base64 image with Selenium and PHP
Install php-webdriver and chromedriver
composer require php-webdriver/webdriver
chromedriver --port=9515 php-webdriver has no Selenium Manager, so it needs a running chromedriver matching your Chrome version. Start it in another terminal and leave it running while the script runs — the examples connect to port 9515, chromedriver’s default, and passing --port=9515 just makes that explicit.
Get the base64 encoded image of some HTML and print to screen
<?php
require __DIR__ . '/vendor/autoload.php';
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
$options = new ChromeOptions();
$options->addArguments(['--headless=new']);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create('http://localhost:9515', $capabilities);
$html = '<html><body><h1>Hello World</h1></body></html>';
$driver->get('data:text/html;charset=utf-8,' . rawurlencode($html));
// takeScreenshot() returns the PNG bytes when no path is given.
echo base64_encode($driver->takeScreenshot());
$driver->quit(); Find out how to generate a PNG from HTML with PHP and Selenium.
That was 22 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.