Guides · PHP · cURL extension

Take a retina-quality (2x) screenshot in PHP with cURL extension

A default screenshot renders at 1 CSS pixel = 1 image pixel, which looks soft on any modern display and falls apart the moment a designer zooms in. "device_scale": 2 renders the page the way a MacBook does — double density — so text is crisp in decks, docs and social cards.

Below is a complete, runnable PHP program using cURL extension, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

<?php
$body = json_encode([
    'url' => "https://your-app.com/report/42",
    'format' => "png",
    'width' => 1280,
    'device_scale' => 2,
]);

$ch = curl_init("https://snapdok.io/v1/render");
$headers = [];
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 90,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('SNAPDOK_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$headers) {
        $parts = explode(':', $line, 2);
        if (count($parts) === 2) $headers[strtolower(trim($parts[0]))] = trim($parts[1]);
        return strlen($line);
    },
]);

$bytes = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);

if ($status !== 200) {
    throw new RuntimeException("snapdok error $status: $bytes");
}
file_put_contents('retina.png', $bytes);

The arithmetic to keep in mind: pixel dimensions are width × device_scale, so 1280 wide at scale 2 produces a 2560-px-wide image with roughly 4× the bytes of the 1× version. For thumbnails that will be displayed small, 1× is cheaper and looks identical after downscaling; spend the scale factor where humans will look closely.

If you arrive from Urlbox or ApiFlash muscle memory: device_scale_factor is accepted as an alias and normalised to the same thing — both spellings even share one cache entry, so mixed codebases do not double-render. JPEG ("format": "jpeg") at scale 2 is often the sweet spot for photography-heavy pages: retina sharpness, a fraction of the PNG size.

Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.

Notes for cURL extension

Plain ext-curl, no Composer needed. The details that bite: CURLOPT_RETURNTRANSFER must be set or the bytes are echoed to stdout instead of returned; the HTTP status has to be read explicitly with curl_getinfo() because cURL happily "succeeds" on a 401; and response headers need a CURLOPT_HEADERFUNCTION callback if you want to check things like X-Form-Fields. file_put_contents is binary-safe in PHP — strings are byte arrays — so no "b" flag dance is needed.

Response headers worth reading

HeaderMeaning
X-CacheBoth device_scale spellings hit the same cache entry.
X-RateLimit-RemainingRequests left in the current second.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with PHP + cURL extension