Anton Kihlström
Anton Kihlström

Reputation: 37

How to screenshot a webpage in php?

I am currently making a website where I simply want users to be able to screenshot a webpage and then download the image. So what I need help with is: how do I screenshot a webpage using PHP then save the image on my server.

I have read a few other tutorials on how to screenshot a page, but I can't get it to work.

I am using a Linux server (Debian 7.0).

Upvotes: 0

Views: 1734

Answers (2)

MerlinTheMagic
MerlinTheMagic

Reputation: 605

I recently published a project that gives PHP access to a browser. Get it here: https://github.com/merlinthemagic/MTS

Like previous answers the project relies on PhantomJS.

After downloading and setup you would simply use the following code:

$myUrl          = "http://www.google.com";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//now do a screenshot, valid formats are png, jpeg, gif.
$imageData      = $windowObj->screenshot("png");

file_put_contents("/path/to/your/file.png", $imageData);

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98901

You can use grabz.it


Take Website Screenshots with PHP

  1. Get your free Application Key and Secret.
  2. Download the free PHP Demo and Library and try it out.

Then use it like this:

include("GrabzItClient.class.php");
$grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
$grabzIt->SetImageOptions("http://www.google.com"); 
$grabzIt->SaveTo("google.jpg");

If you don't want to depend on 3rd parties, you can use phantomjs, i.e.:

phantomjs responsive-screenshot.js http://google.com

Notes:

1- Download responsive-screenshot.js. Check the source code for available options.

2- You can install phantomjs by cloning the github repo:

git clone https://github.com/ariya/phantomjs.git

Upvotes: 1

Related Questions