2023-10-30 08:00:01

网站截图

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


当前回答

你可以使用剪贴画。

KWHTML已弃用,像旧浏览器一样显示页面。

其他回答

有许多开源项目可以生成屏幕截图。例如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

Cutycapt保存网页的大多数图像格式(jpg,png..)下载它从你的突触,它比wkhtmltopdf工作得更好

我用的是bluga。该api允许你每月免费拍摄100张快照,但有时它会为单个页面使用超过1个积分。我刚刚升级了drupal模块,Bluga WebThumbs到drupal7,它允许你在模板或输入过滤器中打印缩略图。

使用这个api的主要优点是,它允许你指定浏览器尺寸,以防你使用自适应css,所以我使用它来获得移动和平板电脑布局以及常规布局的渲染。

有以下语言的api客户端:

PHP, Python, 红宝石, Java, net c#, Perl 和Bash (shell脚本看起来需要perl)

你可以使用剪贴画。

KWHTML已弃用,像旧浏览器一样显示页面。

您可以使用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如果没有。详细信息请查看文档。