2023-10-30 08:00:01

网站截图

有什么方法可以用PHP截图一个网站,然后保存到一个文件?


当前回答

我在Windows上,所以我能够使用imagegrabwindow函数在阅读stephan在这里的提示后。我添加了裁剪(以消除浏览器标题、滚动条等)和调整大小以获得最终图像。这是我的代码。希望这能帮助到别人。

其他回答

不直接。像Selenium这样的软件有这样的特性,可以用PHP控制,但有其他依赖(比如在你想要截图的浏览器的计算机上运行基于java的服务器)

最后编辑:7年过去了,这个答案仍然得到了点赞,但我猜这个答案现在更准确了。


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

否则,你可以使用wkhtmltopdf输出一个html页面在pdf, jpg,无论什么。 接受CSS2.0,使用webkit (safari的包装器)来呈现页面。所以应该没问题。 你必须安装在你的服务器上,以及..

现在,有了新的HTML5和JS特性,也可以使用JavaScript将页面渲染成画布对象。这里有一个很好的库可以做到这一点:Html2Canvas,这里是由同一作者实现的,以获得类似G+的反馈。 一旦将dom渲染到画布中,就可以通过ajax发送到服务器,并将其保存为jpg格式。

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

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

编辑:我现在注意到,wkhtmltopdf团队正在进行另一个项目:wkhtmltoimage,这给你的jpg直接

有许多开源项目可以生成屏幕截图。例如PhantomJS, webkit2png等

这些项目的最大问题是,它们基于旧的浏览器技术,在渲染许多网站时存在问题,特别是在过去几个月/几年里使用webfonts、flexbox、svg和其他各种添加到HTML5和CSS规范的网站。

我尝试了一些第三方服务,大多数都是基于PhantomJS的,这意味着它们的截图质量也很差。生成网站截图的最好的第三方服务是urlbox.io。这是一项付费服务,但也有7天的免费试用,无需承诺任何付费计划。

这里是文档的链接,下面是让它在PHP中使用composer工作的简单步骤。

// 1 . Get the urlbox/screenshots composer package (on command line):
composer require urlbox/screenshots

// 2. Set up the composer package with Urlbox API credentials:
$urlbox = UrlboxRenderer::fromCredentials('API_KEY', 'API_SECRET');

// 3. Set your options (all options such as full page/full height screenshots, retina resolution, viewport dimensions, thumbnail width etc can be set here. See the docs for more.)
$options['url'] = 'example.com';

// 4. Generate the Urlbox url
$urlboxUrl = $urlbox->generateUrl($options);
// $urlboxUrl is now 'https://api.urlbox.io/v1/API_KEY/TOKEN/png?url=example.com'

// 5. Now stick it in an img tag, when the image is loaded in browser, the API call to urlbox will be triggered and a nice PNG screenshot will be generated!
<img src="$urlboxUrl" />

例如,这里是这个页面的全高截图:

https://api.urlbox.io/v1/ca482d7e-9417-4569-90fe-80f7c5e1c781/8f1666d1f4195b1cb84ffa5f992ee18992a2b35e/png?url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F757675%2Fwebsite-screenshots-using-php%2F43652083%2343652083&full_page=true

我最后按照@boksiora的建议使用microweber/screen进行设置。 最初当尝试这里提到的链接时,我得到了什么:

Please download this script from here https://github.com/microweber/screen

我用的是Linux。所以如果你想运行它,你可以根据你的环境调整我的步进。 下面是我在shell上DOCUMENT_ROOT文件夹上做的步骤:

$ sudo wget https://github.com/microweber/screen/archive/master.zip
$ sudo unzip master.zip
$ sudo mv screen-master screen
$ sudo chmod +x screen/bin/phantomjs
$ sudo yum install fontconfig
$ sudo yum install freetype*
$ cd screen
$ sudo curl -sS https://getcomposer.org/installer | php
$ sudo php composer.phar update
$ cd ..
$ sudo chown -R apache screen
$ sudo chgrp -R www screen
$ sudo service httpd restart

将浏览器指向screen/demo/shot.php?url=google.com。当你看到屏幕截图时,你就完成了。更多高级设置的讨论可在这里和这里。

您可以使用简单的无头浏览器,如PhantomJS抓取页面。

你也可以在PHP中使用PhantomJS。

看看这个php脚本。看看这里https://github.com/microweber/screen

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