DEV Community

Cover image for How to Take a Website Screenshot with PHP
alekseykazandaev
alekseykazandaev

Posted on • Originally published at site-shot.com

How to Take a Website Screenshot with PHP

PHP still powers a lot of real-world web applications, so screenshot generation fits naturally into CMS plugins, monitoring tools, and link preview workflows. This Dev.to version keeps the original examples intact while tightening the framing for developers who want a straightforward API-based approach.

Prerequisites

  • PHP 7.4+ with the cURL extension enabled
  • A Site-Shot API key (sign up here)

Basic Screenshot Capture

<?php

$params = http_build_query([
    'url'     => 'https://example.com',
    'userkey' => 'YOUR_API_KEY',
    'width'   => 1280,
    'height'  => 1024,
    'format'  => 'png',
]);

$ch = curl_init("https://api.site-shot.com/?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 70);

$image = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 200 && $image) {
    file_put_contents('screenshot.png', $image);
    echo "Screenshot saved.\n";
} else {
    echo "Error: HTTP {$httpCode}\n";
}
Enter fullscreen mode Exit fullscreen mode

The API returns the raw image bytes. Just save them to a file or serve them to the browser.

Full Page Screenshot

To capture the entire scrollable page:

$params = http_build_query([
    'url'        => 'https://example.com',
    'userkey'    => 'YOUR_API_KEY',
    'full_size'  => 1,
    'max_height' => 15000,
    'format'     => 'png',
]);
Enter fullscreen mode Exit fullscreen mode

JSON Response with Base64

If you need the image as base64 along with HTTP metadata:

<?php

$params = http_build_query([
    'url'           => 'https://example.com',
    'userkey'       => 'YOUR_API_KEY',
    'width'         => 1280,
    'height'        => 1024,
    'format'        => 'png',
    'response_type' => 'json',
]);

$ch = curl_init("https://api.site-shot.com/?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 70);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

if (!empty($data['error'])) {
    die("API error: " . $data['error']);
}

$base64 = $data['image'];
if (strpos($base64, ',') !== false) {
    $base64 = explode(',', $base64, 2)[1];
}

file_put_contents('screenshot.png', base64_decode($base64));
echo "Screenshot saved.\n";
Enter fullscreen mode Exit fullscreen mode

Serving Screenshots in a Web Application

Generate and serve a thumbnail screenshot directly to the browser:

<?php

$targetUrl = $_GET['url'] ?? '';
if (empty($targetUrl)) {
    http_response_code(400);
    die('Missing url parameter');
}

$params = http_build_query([
    'url'          => $targetUrl,
    'userkey'      => 'YOUR_API_KEY',
    'width'        => 1280,
    'height'       => 800,
    'format'       => 'jpeg',
    'scaled_width' => 400,
]);

$ch = curl_init("https://api.site-shot.com/?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 70);

$image = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 200 && $image) {
    header('Content-Type: image/jpeg');
    header('Cache-Control: public, max-age=3600');
    echo $image;
} else {
    http_response_code(502);
    die('Screenshot failed');
}
Enter fullscreen mode Exit fullscreen mode

WordPress Integration

You can integrate the screenshot API into a WordPress plugin or theme:

function site_shot_capture($url, $options = []) {
    $defaults = [
        'url'     => $url,
        'userkey' => defined('SITE_SHOT_API_KEY') ? SITE_SHOT_API_KEY : '',
        'width'   => 1280,
        'height'  => 1024,
        'format'  => 'png',
    ];

    $params = http_build_query(array_merge($defaults, $options));

    $response = wp_remote_get(
        "https://api.site-shot.com/?{$params}",
        ['timeout' => 70]
    );

    if (is_wp_error($response)) {
        return false;
    }

    return wp_remote_retrieve_body($response);
}

// Usage:
$image = site_shot_capture('https://example.com', ['full_size' => 1]);
if ($image) {
    file_put_contents('/tmp/screenshot.png', $image);
}
Enter fullscreen mode Exit fullscreen mode

Summary

Parameter Description Example
url Target web page https://example.com
userkey Your API key abc123
width Viewport width (100–8000) 1280
height Viewport height (100–20000) 1024
full_size Capture full page 1
format Output format png or jpeg
response_type Response format image or json
scaled_width Resize output 400

See the full API documentation for all available parameters.


Try Site-Shot free — no registration needed: https://www.site-shot.com/

Top comments (0)