microweber/screen

一个PHP类,用于与PhantomJs交互并捕获网页屏幕截图

v1.0.6 2017-01-20 15:21 UTC

This package is auto-updated.

Last update: 2024-09-14 19:58:39 UTC


README

基于PHP和PhantomJS的网站截图工具。您可以使用它进行测试或监控服务

安装

通过Composer

$ composer require microweber/screen

如果是在任何Unix系统上,您需要使 bin 目录可执行 chmod +x /path/to/screen/bin/phantomjs

目录 /path/to/screen/jobs 也必须是可写的。

Linux需求

  • FontConfig - apt-get/yum install fontconfig
  • FreeType - apt-get/yum install freetype*

用法

使用这个库,您可以利用PhantomJs来截图网站。

查看我们的演示或阅读以下说明。

创建对象时,您可以在构造函数中传递URL,也可以稍后设置

use Screen\Capture;

$url = 'https://github.com';

$screenCapture = new Capture($url);
// or
$screenCapture = new Capture();
$screenCapture->setUrl($url);

您还可以设置浏览器尺寸

$screenCapture->setWidth(1200);
$screenCapture->setHeight(800);

您还可以设置DOM元素位置(顶部,左侧)

$screenCapture->setTop(100);
$screenCapture->setLeft(100);

这将输出整个页面,包括超出设置尺寸渲染的内容(例如:所有可滚动内容),如果您只想包含这些边界内的内容,则需要裁剪结果

// You also need to set the width and height.
$screenCapture->setClipWidth(1200);
$screenCapture->setClipHeight(800);

一些网页没有将背景颜色设置到body,如果您想设置颜色,可以使用此方法

$screenCapture->setBackgroundColor('#ffffff');

您还可以设置用户代理

$screenCapture->setUserAgentString('Some User Agent String');

并且设置结果图片类型

// allowed types are 'jpg' and 'png', default is 'jpg'.
$screenCapture->setImageType(Screen\Image\Types\Png::FORMAT);
// or
$screenCapture->setImageType('png');
  • 如果格式是 jpg 且未设置背景颜色,默认值将是 #FFFFFF,如果 png 默认背景颜色将是透明的。

最重要的是,保存结果

$fileLocation = '/some/dir/test.' . $screen->getImageType()->getFormat();
$screenCapture->save($fileLocation);

// you don't need to set the file extension
$fileLocation = '/some/dir/test';
$screenCapture->save($fileLocation); // Will automatically determine the extension type

echo $screenCapture->getImageLocation(); // --> /some/dir/test.png

向网页中注入自己的JS

您还可以在截图之前运行自己的JS脚本或片段。

为此,我们有 includeJs 方法,以下是一些使用示例

// Including a remote file
$jQueryUrl = 'https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.1.0/jquery.min.js';
$screenCapture->includeJs(new \Screen\Injection\Url($jQUeryUrl));

// Including a local file
$localFilePath = 'path/to/my/script.js';
$screenCapture->includeJs(new \Screen\Injection\LocalPath($localFilePath));

// Using the scripts included on the library
$screen->includeJs(new \Screen\Injection\Scripts\FacebookHideCookiesPolicy());
$screen->includeJs(new \Screen\Injection\Scripts\FacebookHideSignUp());

// Using a js snippet
$screen->includeJs("console.log('This is supa cool!');");

只需在调用 save(...) 之前使用此方法

传递选项到PhantomJS

您可以设置传递给PhantomJS二进制文件的选项。

$screenCapture->setOptions([
    'ignore-ssl-errors' => 'yes',
    // '--ignore-ssl-errors' => 'yes', // dashes may be omitted
]);

其他配置

除了基本用法外,您还可以设置额外的配置。

您可以更改PhantomJS二进制文件的位置。

$screenCapture->binPath = '/path/to/bin/dir/';
// This will result in /path/to/bin/dir/phantomjs

更改作业位置

$screenCapture->jobs->setLocation('/path/to/jobs/dir/');
echo $screenCapture->jobs->getLocation(); // -> /path/to/jobs/dir/

并设置输出基本位置

$screenCapture->output->setLocation('/path/to/output/dir/');
echo $screenCapture->output->getLocation(); // -> /path/to/output/dir/

// if the output location is setted
$screenCapture->save('file.jpg');
// will save the file to /path/to/output/dir/file.jpg

您还可以像这样清理/删除所有生成的作业文件

$screenCapture->jobs->clean();

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

致谢

感谢PhantomJS (LICENSE) 开发者创造了他们出色的WebKit脚本接口。

此工具最初是为Microweber创建截图的