Capture Screenshot of Website from URL using PHP

Muhammad Umer Farooq
3 min readMar 18, 2019

--

Zest Framework

Web page screenshot capture functionality is used for various purposes in the web application. There are many third party APIs are available to take a screenshot of the website. But if you wish to build your own script to get a screenshot from URL, you can do it easily using PHP using CURL.

There are many solutions to capture screenshots of Web pages. Using the Google Page Speed API is often better because internally Google uses the Chrome browser to capture exactly the way pages are presented to users, as if there was a real user seeing a given Web page. Manuel Lemos

If you like to use screenshotlayer API : https://medium.com/@lablnet/capture-screenshot-of-website-using-php-by-screenshotlayer-api-6adbd8c08263

Get Screenshot of Website from URL

To make web page snapshot, Google PageSpeed Insights API need to be called with the following params.

  • url: specify the URL of the website.
  • screenshot: specify screenshot=true to retrieve the screenshot data.
<?php
$curl_init = curl_init(“https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url={$url}&screenshot=true");
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_init);
curl_close($curl_init);
//call Google PageSpeed Insights API
//decode json data
$googlepsdata = json_decode($response, true);
//screenshot data
$snap = $googlepsdata[‘screenshot’][‘data’];
$snap = str_replace([‘_’, ‘-’], [‘/’, ‘+’], $snap);
echo "<img src=\"data:image/jpeg;base64,".$snap."\" />";

Capture Class

<?phpclass Capture
{
/**
* Capture web screenshot using google api.
*
* @param (string) $url Valid url
*
* @return blob
*/
public function snap($url)
{
//Url value should not empty and validate url
if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) {
$curl_init = curl_init("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url={$url}&screenshot=true");
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_init);
curl_close($curl_init);
//call Google PageSpeed Insights API
//decode json data
$googlepsdata = json_decode($response, true);
//screenshot data
$snap = $googlepsdata['screenshot']['data'];
$snap = str_replace(['_', '-'], ['/', '+'], $snap);
return $snap;
} else {
return false;
}
}
}

Usage

$snap = (new Capture())->snap('https://zestframework.xyz');
if ($snap)
echo "<img src=\"data:image/jpeg;base64,".$snap."\" />";
else
echo "Enter valid url";

Complete example

<?phpclass Capture
{
/**
* Capture web screenshot using google api.
*
* @param (string) $url Valid url
*
* @return blob
*/
public function snap($url)
{
//Url value should not empty and validate url
if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) {
$curl_init = curl_init("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url={$url}&screenshot=true");
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_init);
curl_close($curl_init);
//call Google PageSpeed Insights API
//decode json data
$googlepsdata = json_decode($response, true);
//screenshot data
$snap = $googlepsdata['screenshot']['data'];
$snap = str_replace(['_', '-'], ['/', '+'], $snap);
return $snap;
} else {
return false;
}
}
}
$snap = (new Capture())->snap('https://zestframework.xyz');
if ($snap)
echo "<img src=\"data:image/jpeg;base64,".$snap."\" />";
else
echo "Enter valid url";

Example with form

<?phpclass Capture
{
/**
* Capture web screenshot using google api.
*
* @param (string) $url Valid url
*
* @return blob
*/
public function snap($url)
{
//Url value should not empty and validate url
if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) {
$curl_init = curl_init("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url={$url}&screenshot=true");
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl_init);
curl_close($curl_init);
//call Google PageSpeed Insights API
//decode json data
$googlepsdata = json_decode($response, true);
//screenshot data
$snap = $googlepsdata['screenshot']['data'];
$snap = str_replace(['_', '-'], ['/', '+'], $snap);
return $snap;
} else {
return false;
}
}
}
if (isset($_POST['submit']) && !empty($_POST['url'])) {
$snap = (new Capture())->snap($_POST['url']);
if ($snap)
echo "<img src=\"data:image/jpeg;base64,".$snap."\" />";
else
echo "Enter valid url";
}
?>
<form method="post" action="" >
<p>Website URL: <input type="text" name="url" value="" /></p>
<input type="submit" name="submit" value="CAPTURE">
</form>

Conclusion

In this post you have learn about how you can capture the website Screenshort/Snap in php by using Google Page Speed API, also we built simple class for performing this to able to reuse our code

gist: https://gist.github.com/Lablnet/a43c1e04ce508c95e7beafb70a7a1459

github: https://github.com/Lablnet/PHP-URL

phpclasses:https://www.phpclasses.org/package/10655-PHP-Manipulate-URLs-and-capture-a-screenshot-of-a-page.html

--

--

Muhammad Umer Farooq

Hi My name is Muhammad Umer Farooq, After spending two years in mordern develpment i confidently say that I am how expert in PHP.