Login |  Register 
I've learnt so much after subscribing to read your exclusive articles!
Nick
 

Take Screenshots with PHP!

What can be one of the more complicated tasks in PHP is taking screenshots of websites, however since GrabzIt has started to run thier free service this has become a lot easier.

First of all you will need the latest GrabzIt PHP libary. This needs to be installed at a web accessible location in order to work. You will also need to ensure that the images directory has write permissions.

This contains all the files you need including an example index.php file that allows you to take the picture and the handler to recieve it. If you want to get started quickly stop here and update the variables in the GrabzItConfig.php to match the location you installed the libary.

If you want a more detailed understanding of how GrabzIt works then continue! First there is a PHP page that is going to request the screenshots. This will need to include the GrabzItClient class file and create a client as shown below.

include("GrabzItClient.class.php");

$grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");

You can get your application key and application secret from GrabzIt

Next this file will need to tell the GrabzIt service to take a screenshot, in the example below we are going to take a screenshot of google...

$id = $grabzIt->TakePicture("http://www.google.com", "http://www.example.com/GrabzItHandler.php");

The $id is the unique id of that particular screenshot and is used by the GetPicture method if you want to manually get the picture from GrabzIt. However we will be using the automatic handler method. Notice http://www.example.com/GrabzItHandler.php that the handler location is passed through. Change this so it matches the location of the handler on your website.

No we must change the handler to save the screenshot correctly on the website.

include("GrabzItClient.class.php");

$message = $_GET["message"];
$customId = $_GET["customid"];
$id = $_GET["id"];
$filename = $_GET["filename"];

//Custom id can be used to store user ids or whatever is needed for the later processing of the
//resulting screenshot

$grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
$result = $grabzIt->GetPicture($id);

if (!$result)
{
 return;
}

//Ensure that the application has the correct rights for this directory.
file_put_contents("images" . DIRECTORY_SEPARATOR . $filename, $result);

The only really important thing to change is the file_put_contents to ensure it is writing the screenshot image to the correct location.

Thats it, your website is now taking screenshots!


Comments to date: 6. Page 1 of 1. Average Rating:
RahulKANNAUJIYA   2:26am on Saturday, July 9th, 2016 
GOOD BUT NOT STUSIFIED
rahul   2:25am on Saturday, July 9th, 2016 
dffd
jonty   2:27am on Monday, February 15th, 2016 
Good
ram   11:53pm on Monday, October 12th, 2015 
good
aaaaa   3:58am on Saturday, February 21st, 2015 
aaaaaa
iiiii   7:26am on Sunday, December 22nd, 2013 
jjhjhjj

security image
Written by Dominic Skinner
Last Updated: 2012-05-10 04:03:02