有什么方法可以用PHP截图一个网站,然后保存到一个文件?
当前回答
有许多开源项目可以生成屏幕截图。例如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
其他回答
我在Windows上,所以我能够使用imagegrabwindow函数在阅读stephan在这里的提示后。我添加了裁剪(以消除浏览器标题、滚动条等)和调整大小以获得最终图像。这是我的代码。希望这能帮助到别人。
自从PHP 5.2.2以来,完全可以用PHP捕获一个网站!
imagegrabscreen -捕获整个屏幕
<?php
$img = imagegrabscreen();
imagepng($img, 'screenshot.png');
?>
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');
?>
编辑:注意,这些功能只在Windows系统上可用!
好吧,PhantomJS是一个可以很容易地放在服务器上并与php集成的浏览器。您可以在WDudes中找到代码。他们包括了更多的功能,如指定图像大小,缓存,下载为文件或显示在img src等。
<img src=”screenshot.php?url=google.com” />
URL参数
宽度和高度:截图.php?url=google.com&w=1000&h=800 裁剪: screenshot.php ? url = google.com&w = 1000 h = 800 &clipw = 800 &cliph = 600 关闭缓存并加载新截屏: screenshot.php ? url = google.com&cache = 0 下载图片:screenshot.php?url=google.com&download=true
你可以在这里看到教程:使用PHP不带API抓取一个网站的截图
Cutycapt保存网页的大多数图像格式(jpg,png..)下载它从你的突触,它比wkhtmltopdf工作得更好
您可以使用https://grabz.it解决方案。
它有一个非常灵活的PHP API,可以以不同的方式调用,例如从cronjob或PHP网页。
为了实现它,你首先需要获得应用密钥和秘密,并下载(免费的)SDK。
并给出了实现的实例。首先初始化:
include("GrabzItClient.class.php");
// Create the GrabzItClient class
// Replace "APPLICATION KEY", "APPLICATION SECRET" with the values from your account!
$grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
截图示例:
// To take a image screenshot
$grabzIt->URLToImage("http://www.google.com");
// Or to take a PDF screenshot
$grabzIt->URLToPDF("http://www.google.com");
// Or to convert online videos into animated GIF's
$grabzIt->URLToAnimation("http://www.example.com/video.avi");
// Or to capture table(s)
$grabzIt->URLToTable("http://www.google.com");
其次是储蓄。你可以使用两个保存方法之一,保存如果公共可访问的回调句柄可用,SaveTo如果没有。详细信息请查看文档。
推荐文章
- 如何实现一个好的脏话过滤器?
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PHP中获得变量名作为字符串?
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 在PHP中对动态变量名使用大括号
- 如何从对象数组中通过对象属性找到条目?
- 如何从关联数组中删除键及其值?
- PHP字符串中的花括号