有什么方法可以用PHP截图一个网站,然后保存到一个文件?
当前回答
我最后按照@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, 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
不直接。像Selenium这样的软件有这样的特性,可以用PHP控制,但有其他依赖(比如在你想要截图的浏览器的计算机上运行基于java的服务器)
我在Windows上,所以我能够使用imagegrabwindow函数在阅读stephan在这里的提示后。我添加了裁剪(以消除浏览器标题、滚动条等)和调整大小以获得最终图像。这是我的代码。希望这能帮助到别人。
我最后按照@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。当你看到屏幕截图时,你就完成了。更多高级设置的讨论可在这里和这里。
Cutycapt保存网页的大多数图像格式(jpg,png..)下载它从你的突触,它比wkhtmltopdf工作得更好
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?