Website screenshots
Is there any way of taking a screenshot of a website in PHP, then saving it to a file?
LAST EDIT: after 7 years I'm still getting upvotes for this answer, but I guess this one is now much more accurate.
Sure you can, but you'll need to render the page with something. If you really want to only use php, I suggest you HTMLTOPS, which renders the page and outputs it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and don't support all the CSS).
Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whatever.. Accept CSS2.0, use the webkit (safari's wrapper) to render the page.. so should be fine. You have to install it on your server, as well..
UPDATE Now, with new HTML5 and JS feature, is also possible to render the page into a canvas object using JavaScript. Here a nice library to do that: Html2Canvas and here is an implementation by the same author to get a feedback like G+. Once you have rendered the dom into the canvas, you can then send to the server via ajax and save it as a jpg.
EDIT: You can use the imagemagick tool for transforming pdf to png. My version of wkhtmltopdf does not support images. E.g. convert html.pdf -append html.png
.
EDIT: This small shell script gives a simple / but working usage example on linux with php5-cli and the tools mentioned above.
EDIT: i noticed now that the wkhtmltopdf team is working on another project: wkhtmltoimage, that gives you the jpg directly
Since PHP 5.2.2 it is possible, to capture a website with PHP solely!
imagegrabscreen — Captures the whole screen
<?php
$img = imagegrabscreen();
imagepng($img, 'screenshot.png');
?>
imagegrabwindow - Grabs a window or its client area using a windows handle (HWND property in COM instance)
<?php
$Browser = new COM('InternetExplorer.Application');
$Browserhandle = $Browser->HWND;
$Browser->Visible = true;
$Browser->Fullscreen = true;
$Browser->Navigate('http://www.stackoverflow.com');
while($Browser->Busy){
com_message_pump(4000);
}
$img = imagegrabwindow($Browserhandle, 0);
$Browser->Quit();
imagepng($img, 'screenshot.png');
?>
Edit: Note, these functions are available on Windows systems ONLY!
If you don't want to use any third party tools, I have come across to simple solution that is using Google Page Insight api.
Just need to call it's api with params screenshot=true
.
https://www.googleapis.com/pagespeedonline/v1/runPagespeed?
url=https://stackoverflow.com/&key={your_api_key}&screenshot=true
For mobile site view pass &strategy=mobile
in params,
https://www.googleapis.com/pagespeedonline/v1/runPagespeed?
url=http://stackoverflow.com/&key={your_api_key}&screenshot=true&strategy=mobile
DEMO.
You can use simple headless browser like PhantomJS to grab the page.
Also you can use PhantomJS with PHP.
Check out this little php script that do this. Take a look here https://github.com/microweber/screen
And here is the API- http://screen.microweber.com/shot.php?url=https://stackoverflow.com/questions/757675/website-screenshots-using-php
This ought to be good for you:
https://wkhtmltopdf.org/
Make sure you download the wkhtmltoimage distribution!