Capture page screenshot by URL php script

Capture page screenshot by URL php script

There are many third-party API available to capture screenshot of a page or website. For this I will used simple API `Google PageSpeed Insights API`, which is used to measure the performance of website. It will create a website screenshot when user try to check performance of any website. I will used file_get_contents() function in php to capture website screenshot. Website or page URL is required to pass in this function as a parameter. Use below code to take a website or page screenshot. Replace $URL variable value with your website link which you want to capture.


//page or website URL which you wants to capture
$siteURL = "";
$jsonData = file_get_contents("$siteURL&screenshot=true");

//decode $jsonData json data
$jsonData = json_decode($jsonData, true);

//Website data in array $jsonData
// Image data key is in $jsonData['screenshot']['data'];
$screenshot = $jsonData['screenshot']['data'];

//change some characters

$screenshot = str_replace(array('_','-'),array('/','+'),$screenshot); 

//pass image data in img src

echo "<img src=\"data:image/jpeg;base64,".$screenshot."\" />";

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

David Richard
David Richard
20 Dec 2020

I have used your code to create thumbnails for my pages and posts Thanks

Daniel rikson
Daniel rikson
21 Dec 2020

Thanks buddy.. Response is in base64, I've saved the image using file_puts_content() function

Add your comment