Website screenshots

有什么方法在PHP中截取网站截图,然后将其保存到文件中?


LAST EDIT : after 7 years im still getting upvote fot 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, wich render the page and output it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and dont support all the CSS).

Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whaterver.. 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. Eg 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=www.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=www.stackoverflow.com/&key={your_api_key}&screenshot=true&strategy=mobile

DEMO .

链接地址: http://www.djcxy.com/p/68146.html

上一篇: 向UIView添加渐变的现代技术

下一篇: 网站截图