This is a simple experiment for capture website screenshots and most definitely not pretty to look at but it does give a basic overview of how it can be done using the Google Pagespeed API ver 5.
You are welcome to use it if you want, but please no hotlinking.
The image files are cached for a number of days so they could be deleted at any time.
Error checking is a minimum so make sure you enter a fully qualified working URL.
A successful screenshot depends on successful access to the target website by the bot. If your URL somehow block certain “things” from accessing your site, you will get a blank screenshot.
If you use the “Recapture Screenshot” option to replace a previously captured image, you will need to press “CTRL + F5”, in the case of windows, to force a refresh and reload of the images.
Any feedback and suggestions for improvement are welcome.
Download the Generate Website Screenshots with php script.
config_file.php
/*version Ver. 0.6 21-5-2019*/ $folder_name = 'screenshots/'; //folder will be created if not existing. $_chmod = 0755; $file_type = 'jpg'; // png, jpg, gif, WebP $cache_duration = '10'; //days. Set to 0 to prevent caching $expire_cache = '0'; // 0 = Delete cache for called URL, 1 = Clear cache for all screenshots when script runs. $show_image = '0'; // 0 = Display the generated image, 1 = Redirect to generated screenshot, 2 = Redirect back to referring page $strategy = 'desktop'; // screenshot in desktop or mobile format $apiKey = 'REPLACE_WITH_YOUR_GOOGLE_API_KEY'// Your google API key
xms_systems_screenshot_maker.php
/* screenShot class - Capture webpage screenshots version Ver. 0.6 21-5-2019 Copyright (c) 2019, Written by Fred Mac Donald sales at xms-systems.co.uk This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/ */ header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); clearstatcache();// Prevent browser caching include 'xms_systems_screenshot_maker_config.php'; function XMSWebsiteScreenshot($url){ global $strategy, $apiKey; $api_response = file_get_contents("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$url&screenshot=true&strategy=$strategy&key=$apiKey"); //decode json data $result = json_decode($api_response, true); //screenshot data $screenshot = $result['lighthouseResult']['audits']['final-screenshot']['details']['data']; $screenshot = str_replace(array('_','-'),array('/','+'),$screenshot); return $screenshot; } function getFileName() { if(!empty($_GET['url'])) { global $file_type; $url = $_GET['url']; $url = rtrim($url,"/"); //remove trailing slash from url if (filter_var($url, FILTER_VALIDATE_URL)) { $urlHost = parse_url($url, PHP_URL_HOST);// if full qualified URL, pull out the domain name $urlPath = parse_url($url, PHP_URL_PATH);// if full qualified URL, pull out the file name $urlFragment = parse_url($url, PHP_URL_FRAGMENT);// if full qualified URL, pull out the Query String $url = $urlHost.$urlPath.$urlFragment;//reassemble the bits to create a unique filename $arr = array("http" => "","https" => "","://" => "","www." => "","." => "-","/" => "-");// for cleaning the filename $url = strtr($url,$arr);// clean the filename } $file_name = $url.'.'.$file_type;// create filename } return $file_name; } function checkFile($file) { global $cache_duration, $folder_name, $expire_cache; //Get creation date and calculate deletion date $creation_date = date('Y-m-d',filemtime($folder_name.$file)); $next_due_date = date('Y-m-d', strtotime($creation_date. ' +'.$cache_duration.' days')); // Check if due for delete $today = date('Y-m-d'); //Convert them to timestamps. $today = strtotime($today); $next_due_date = strtotime($next_due_date); //Calculate the difference. $difference = $next_due_date - $today; if ($difference <= '0') { unlink($folder_name.$file); } } function delCache($expire_cache,$cache_duration,$folder_name) { global $cache_duration, $folder_name, $expire_cache; if ($expire_cache == '1')// Delete all files older than cache { $dh = opendir( $folder_name ); $file = readdir( $dh ); while( $file !== FALSE ) { $file_length = strlen($file); if ($file_length > '4') { checkFile($file); } $file = readdir( $dh ); } }// End Delete all files older than cache else// Check if URL file exists and older than cache { $file = getFileName(); checkFile($file); } } delCache(); if(!empty($_GET['url'])) { $force_new = $_GET['new']; //Create filename $file_name = getFileName(); //Check folder if (!file_exists($folder_name)) { mkdir($folder_name);// create folder chmod($folder_name, $_chmod);// set permissions } //Check if Thumbnail exists if ((file_exists($folder_name.$file_name)) && ($force_new != '1')) { $folder_name = preg_replace('/^\W+|\W+$/', '', $folder_name);// check only actual folders and not . or .. in tree $img = 'https://'.$_SERVER['HTTP_HOST'].'/'.$folder_name.'/'.$file_name; if ($show_image == '0') { ?> <p><img src="<?php echo $img;?>" border="1" /></p> <p>"<?php echo $img;?>"<br> <strong>XMS Systems Screenshot Generator. Ver. 0.6</strong> </p> <?php }elseif ($show_image == '1') { header("refresh:2;url=".$img ); echo "Have valid Screenshot.<br />Will redirect in 5 seconds"; exit; }else { header('Location: ' . $_SERVER['HTTP_REFERER']); exit; } }else//Generate Thumbnail { $url = $_GET['url']; $url = 'http://' . preg_replace('#^.*://#', '', $url); // strip any protocol and add http:// to allow for url's entered without protocol $url = urlencode($url); $file = XMSWebsiteScreenshot($url); $img = str_replace('data:image/jpeg;base64,', '', $file); file_put_contents($folder_name.$file_name, base64_decode($img));//Save File $folder_name = preg_replace('/^\W+|\W+$/', '', $folder_name); $img = 'https://'.$_SERVER['HTTP_HOST'].'/'.$folder_name.'/'.$file_name; if ($show_image == '0') { ?> <img src="<?php echo $img;?>" border="1" /> <p>"<?php echo $img;?>"<br> <strong>XMS Systems Screenshot Generator. Ver. 0.6</strong></p> <?php }elseif ($show_image == '1') { header("refresh:5;url=".$img ); echo "New Screenshot Generated.<br />Will redirect in 5 seconds"; exit; }else { header('Location: ' . $_SERVER['HTTP_REFERER']); exit; } } }else { echo 'Go away ;-)'; } //Call: http://translate.leadingwebexposure.com/xms_systems_screenshot_maker.php?url=https://www.xms-systems.co.uk&new=1