php 网站截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/757675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:46:21  来源:igfitidea点击:

Website screenshots

phpscreenshot

提问by

Is there any way of taking a screenshot of a website in PHP, then saving it to a file?

有没有办法用PHP截取网站的屏幕截图,然后将其保存到文件中?

回答by Strae

LAST EDIT: after 7 years I'm still getting upvotes for this answer, but I guess this oneis now much more accurate.

最后一次编辑:7 年后,我仍然为这个答案投票,但我想这个答案现在更准确了。



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).

当然可以,但是你需要用一些东西来渲染页面。如果您真的只想使用 php,我建议您使用HTMLTOPS,它会呈现页面并将其输出到 ps 文件(ghostscript)中,然后将其转换为 .jpg、.png、.pdf ..复杂的页面(并且不支持所有的 CSS)。

Else, you can use wkhtmltopdfto 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..

否则,您可以使用wkhtmltopdf以 pdf、jpg 等格式输出 html 页面。接受 CSS2.0,使用 webkit(safari 的包装器)来呈现页面。所以应该没问题。您还必须将它安装在您的服务器上。

UPDATENow, 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: Html2Canvasand here is an implementationby 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.

更新现在,借助新的 HTML5 和 JS 功能,还可以使用 JavaScript 将页面呈现到画布对象中。这是一个很好的库来做到这一点:Html2Canvas这里是同一作者的一个实现,以获得像 G+ 这样的反馈。将 dom 渲染到画布中后,您可以通过 ajax 将其发送到服务器并将其保存为 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.

编辑:您可以使用 imagemagick 工具将 pdf 转换为 png。我的 wkhtmltopdf 版本不支持图像。例如convert html.pdf -append html.png

EDIT: This small shell scriptgives a simple / but working usage example on linux with php5-cli and the tools mentioned above.

编辑这个小 shell 脚本在 linux 上使用 php5-cli 和上面提到的工具提供了一个简单/但有效的用法示例。

EDIT: i noticed now that the wkhtmltopdf team is working on another project: wkhtmltoimage, that gives you the jpg directly

编辑:我现在注意到 wkhtmltopdf 团队正在研究另一个项目:wkhtmltoimage,它直接为您提供 jpg

回答by stephan

Since PHP 5.2.2 it is possible, to capture a website with PHP solely!

从 PHP 5.2.2 开始,可以单独使用 PHP 捕获网站

imagegrabscreen— Captures the whole screen

imagegrabscreen— 捕获整个屏幕

<?php
$img = imagegrabscreen();
imagepng($img, 'screenshot.png');
?>

imagegrabwindow- Grabs a window or its client area using a windows handle (HWND property in COM instance)

imagegrabwindow- 使用窗口句柄(COM 实例中的 HWND 属性)抓取窗口或其客户区

<?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!

编辑:注意,这些功能仅在 Windows 系统上可用!

回答by Rikesh

If you don't want to use any third party tools, I have come across to simple solution that is using Google Page Insightapi.

如果您不想使用任何第三方工具,我遇到了使用Google Page Insightapi 的简单解决方案。

Just need to call it's api with params screenshot=true.

只需要用 params 调用它的 api 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=mobilein params,

对于移动站点视图传递&strategy=mobile参数,

https://www.googleapis.com/pagespeedonline/v1/runPagespeed?
url=http://stackoverflow.com/&key={your_api_key}&screenshot=true&strategy=mobile

DEMO.

演示

回答by boksiora

You can use simple headless browser like PhantomJS to grab the page.

你可以使用像 PhantomJS 这样简单的无头浏览器来抓取页面。

Also you can use PhantomJS with PHP.

您也可以将 PhantomJS 与 PHP 一起使用。

Check out this little php script that do this. Take a look here https://github.com/microweber/screen

查看这个执行此操作的小 php 脚本。看看这里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

这是 API- http://screen.microweber.com/shot.php?url=https://stackoverflow.com/questions/757675/website-screenshots-using-php

回答by Christian

This ought to be good for you:

这应该对你有好处:

https://wkhtmltopdf.org/

https://wkhtmltopdf.org/

Make sure you download the wkhtmltoimage distribution!

确保您下载 wkhtmltoimage 发行版!

回答by majkinetor

Yes. You will need some things tho:

是的。您将需要一些东西:

See khtmld(aemon)on *nx. See Url2Jpgfor Windows but since it is dotNet app you should also chek Url2Bmp

请参阅*nx 上的khtmld(aemon)。请参阅适用于 Windows 的Url2Jpg,但由于它是 dotNet 应用程序,因此您还应该检查 Url2Bmp

Both are console tools that u can utilise from your web app to get the screenshot.

两者都是控制台工具,您可以从您的网络应用程序中使用它们来获取屏幕截图。

There are also web services that offer it. Check thisout for example.

也有提供它的网络服务。检查这个出来的例子。

Edit:

编辑:

This linkis useful to.

这个链接很有用。

回答by Adam Davis

It's in Python, but going over the documentation and code you can see exactly how this is done. If you can run python, then it's a ready-made solution for you:

它是在 Python 中编写的,但是通过查看文档和代码,您可以确切地看到这是如何完成的。如果你可以运行python,那么它就是一个现成的解决方案:

http://browsershots.org/

http://browsershots.org/

Note that everything can run on one machine for one platform, or one machine with virtual machines running the other platforms.

请注意,一切都可以在一台机器上针对一个平台运行,或者在一台机器上运行其他平台的虚拟机。

Free, open source, scroll to bottom of page for links to documentation, source code, and other information.

免费、开源,滚动到页面底部以获取指向文档、源代码和其他信息的链接。

回答by Mee

Yes it is. If you only need image of URL try this

是的。如果你只需要 URL 的图像试试这个

<img src='http://zenithwebtechnologies.com.au/thumbnail.php?url=www.subway.com.au'>

Pass the url as argument and you'll get the image for more details check this link http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html

将 url 作为参数传递,您将获得图像以获取更多详细信息,请查看此链接http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html

回答by Gijo Varghese

Well, PhantomJS is a browser that can be easily put on a server and integrate it to php. You can find the code in WDudes. They have included lot more features like specifying the image size, cache, download as a file or display in img src etc.

好吧,PhantomJS 是一个浏览器,可以很容易地放在服务器上并将其集成到 php 中。您可以在 WDudes 中找到代码。它们包含了更多功能,例如指定图像大小、缓存、下载为文件或在 img src 中显示等。

<img src=”screenshot.php?url=google.com” />

URL Parameters

网址参数

  • Width and Height: screenshot.php?url=google.com&w=1000&h=800

  • With cropping: screenshot.php?url=google.com&w=1000&h=800&clipw=800&cliph=600

  • Disable cache and load fresh screesnhot:
    screenshot.php?url=google.com&cache=0

  • To download the image: screenshot.php?url=google.com&download=true

  • 宽高:screenshot.php?url=google.com&w=1000&h=800

  • 裁剪:screenshot.php?url=google.com&w=1000&h=800&clipw=800&cliph=600

  • 禁用缓存并加载新的 screesnhot:
    screenshot.php?url=google.com&cache=0

  • 下载图片:screenshot.php?url=google.com&download=true

You can see the tutorial here: Capture Screenshot of a Website using PHP without API

您可以在此处查看教程:Capture Screenshot of a Website using PHP without API

回答by Rinku

I always use microweber screento capture screenshot of any webpage. Here we can find a well written tutorial. This is easier and should not take more than 3 minutes to learn.

我总是使用microweber 屏幕来捕获任何网页的屏幕截图。在这里我们可以找到一个写得很好的教程。这更容易,学习时间不应超过 3 分钟。