Last updated 3 months ago

Convert HTML/CSS to an image with PHP.

Here we will show you how to generate an image from HTML/CSS with PHP.

This example uses the Guzzle library. Install instructions are here.

Example code

require 'vendor/autoload.php';
$html = "<div class='ping'>Pong ✅</div>";
$css = ".ping { padding: 20px; font-family: 'sans-serif'; }";
$client = new GuzzleHttpClient();
// Retrieve your user_id and api_key from
$res = $client->request('POST', '', [
'auth' => ['user_id', 'api_key'],
'form_params' => ['html' => $html, 'css' => $css]
echo $res->getBody();
// {"url":""}

More examples

For more advanced examples, take a look here.