How to make screenshots in PHP

Taking a screenshot of your website is very straightforward using ScrapingBee. You can either take a screenshot of the visible portion of the page, the whole page, or an element of the page.

That can be done by specifying one of these parameters with your request:

  • screenshot to true or false.
  • screenshot_full_page to true or false.
  • screenshot_selector to the CSS selector of the element.

In this tutorial, we will see how to take a screenshot of ScrapingBee’s blog using the three methods. 

1. Using screenshot parameter:

The code below will take a screenshot of the blog home page:

<?php

// Get cURL resource
$ch = curl_init();

// Set base url & API key
$BASE_URL = "https://app.scrapingbee.com/api/v1/?";
$API_KEY = "YOUR-API-KEY";

// Set parameters
$parameters = array(
    'api_key' => $API_KEY,
    'url' => 'https://scrapingbee.com/blog', // The URL to scrape
    'screenshot' => true // Set screenshot parameter to true
);
// Building the URL query
$query = http_build_query($parameters);

// Set the URL for cURL
curl_setopt($ch, CURLOPT_URL, $BASE_URL.$query);

// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Send the request and save response to $response
$response = curl_exec($ch);

// Stop if fails
if (!$response) {
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
$file = fopen("screenshot.png", "w");
fwrite($file, $response);
fclose($file);

// Close curl resource to free up system resources
curl_close($ch);
?>

As you’ll see below, the screenshot only covered the visible part of the scraper [1920x1080]. It didn’t cover the blog posts, or the footer.

So what affects the size of the image, and the visible parts of the page? The answer is the scraper’s default viewport. Its default width and height are 1920px and 1080px respectively. And to change them we’ll have to make the request with two additional parameters: window_width and window_height.

<?php

// Get cURL resource
$ch = curl_init();

// Set base url & API key
$BASE_URL = "https://app.scrapingbee.com/api/v1/?";
$API_KEY = "YOUR-API-KEY";

// Set parameters
$parameters = array(
    'api_key' => $API_KEY,
    'url' => 'https://scrapingbee.com/blog', // The URL to scrape
    'screenshot' => true, // Set screenshot parameter to true
    'window_width' => '1280', // Set viewport width to 1280 pixels
    'window_height' => '720' // Set viewport height to 1280 pixels
);
// Building the URL query
$query = http_build_query($parameters);

// Set the URL for cURL
curl_setopt($ch, CURLOPT_URL, $BASE_URL.$query);

// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Send the request and save response to $response
$response = curl_exec($ch);

// Stop if fails
if (!$response) {
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
$file = fopen("screenshot.png", "w");
fwrite($file, $response);
fclose($file);

// Close curl resource to free up system resources
curl_close($ch);
?>

And as you will see, the image below is 1280x720 pixels.

2. Using screenshot_full_page parameter:

This parameter makes the request take a screenshot of the full page. Here’s how to use it:

​​​​​​​<?php

// Get cURL resource
$ch = curl_init();

// Set base url & API key
$BASE_URL = "https://app.scrapingbee.com/api/v1/?";
$API_KEY = "YOUR-API-KEY";

// Set parameters
$parameters = array(
    'api_key' => $API_KEY,
    'url' => 'https://scrapingbee.com/blog', // The URL to scrape
    'screenshot_full_page' => true // Set screenshot_full_page parameter to true
);
// Building the URL query
$query = http_build_query($parameters);

// Set the URL for cURL
curl_setopt($ch, CURLOPT_URL, $BASE_URL.$query);

// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Send the request and save response to $response
$response = curl_exec($ch);

// Stop if fails
if (!$response) {
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
$file = fopen("screenshot.png", "w");
fwrite($file, $response);
fclose($file);

// Close curl resource to free up system resources
curl_close($ch);
?>

And the results as you’ll see, is a screenshot of the whole page. Large screenshot, click here to see it.

3. Using screenshot_selector parameter:

This parameter will take a screenshot of any HTML element on the page. All you have to do, is to specify the CSS selector of that element.

Let’s say that we wanted to take a screenshot of the footer section on this page. We will simply make our request with this additional parameter: screenshot_selector="footer".

So the code will look like this:

<?php

// Get cURL resource
$ch = curl_init();

// Set base url & API key
$BASE_URL = "https://app.scrapingbee.com/api/v1/?";
$API_KEY = "YOUR-API-KEY";

// Set parameters
$parameters = array(
    'api_key' => $API_KEY,
    'url' => 'https://scrapingbee.com/blog', // The URL to scrape
    'screenshot_selector' => 'footer' // Set screenshot_selector parameter to 'footer' which is the CSS selector of our footer
);
// Building the URL query
$query = http_build_query($parameters);

// Set the URL for cURL
curl_setopt($ch, CURLOPT_URL, $BASE_URL.$query);

// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Send the request and save response to $response
$response = curl_exec($ch);

// Stop if fails
if (!$response) {
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
$file = fopen("screenshot.png", "w");
fwrite($file, $response);
fclose($file);

// Close curl resource to free up system resources
curl_close($ch);
?>

And the result is a screenshot of the footer as you can see: 

 

Go back to tutorials