2023-10-30 08:00:01

网站截图

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


当前回答

好吧,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抓取一个网站的截图

其他回答

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

你可以使用剪贴画。

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

您可以使用简单的无头浏览器,如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

好吧,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抓取一个网站的截图