php 如何使用php截取网页的屏幕截图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40706233/
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-26 02:13:49  来源:igfitidea点击:

how to take a screenshot of a webpage using php?

phphtmlwebscreenshot

提问by new_one

I need to take screenshots of few web pages and save them in a folder using php.

我需要截取几个网页的屏幕截图并使用 php 将它们保存在一个文件夹中。

I want to do this using a php script, is there a way I can render those webpages and take screenshot of a particular area after the website is fully loadedd? - I'm using windows.

我想使用 php 脚本执行此操作,有没有办法在网站完全加载后呈现这些网页并截取特定区域的屏幕截图?- 我正在使用窗户。

I tried using wkhtmltoimage but it takes low quality screenshots and also it takes the screenshot before the whole page is loaded, so some images of the webpage are not appearing in the screenshot, is there a way to do this using php?

我尝试使用 wkhtmltoimage 但它会截取低质量的屏幕截图,并且还会在整个页面加载之前截取屏幕截图,因此屏幕截图中没有显示网页的某些图像,有没有办法使用 php 执行此操作?

Please help php specialists. Thank you

请帮助php专家。谢谢

回答by Adriano Moutinho

PHP runs on server side, so to be able to take screenshots you need something to render HTML (a browser-like script) and generate a image of it. This script will do that (wkhtmltoimage too):

PHP 在服务器端运行,因此为了能够截取屏幕截图,您需要一些东西来呈现 HTML(类似浏览器的脚本)并生成它的图像。这个脚本会做到这一点(wkhtmltoimage 也是):

HTML2PS: http://freecode.com/projects/html2ps_php

HTML2PS:http://freecode.com/projects/html2ps_php

But I still think the best solution will be using javascript. Of course, only the client will have access to the screenshots, and maybe you can upload the image to the server. A client browser is better than any kind of server script.

但我仍然认为最好的解决方案是使用 javascript。当然,只有客户端才能访问屏幕截图,也许您可​​以将图像上传到服务器。客户端浏览器比任何类型的服务器脚本都要好。

If that is the case, I would suggest using HTML2CANVAS:

如果是这种情况,我建议使用 HTML2CANVAS:

HTML2CANVAS: http://html2canvas.hertzen.com/

HTML2CANVAS:http://html2canvas.hertzen.com/

The best server solution will be a linux distribution with a google chrome installation and a local script to take screenshots. That is the only way to get accurate screenshots of a web page that may contain javacript/HTML5/animations and other difficult to render stuff.

最好的服务器解决方案将是带有谷歌浏览器安装和本地脚本的 linux 发行版来截取屏幕截图。这是获取可能包含 javacript/HTML5/animations 和其他难以呈现的内容的网页的准确屏幕截图的唯一方法。

回答by Karol Gasienica

JavaScript / HTML5 / Canvas

JavaScript / HTML5 / 画布

It could be hard to code it only in PHP. In your case I think I would follow those instructions:

仅使用 PHP 可能很难对其进行编码。在你的情况下,我想我会遵循这些说明:

Using HTML5/Canvas/JavaScript to take screenshots

使用 HTML5/Canvas/JavaScript 截取屏幕截图

PHP

PHP

Or if you really want use only PHP, it should help you:

或者,如果您真的只想使用 PHP,它应该可以帮助您:

Website screenshots using PHP

使用PHP的网站截图