cpdrenato/screen

用于与 PhantomJs 交互并捕获 64 位截图的 PHP 类

v1.0.6 2021-02-17 13:48 UTC

This package is not auto-updated.

Last update: 2024-09-27 13:34:07 UTC


README

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

64 位 PhantomJS 版本

    $ cd vendor/cpdrenato/screen/bin/
    $ file phantomjs
    $ ./phantomjs

安装

通过 Composer

$ composer require cpdrenato/screen

如果在任何 Unix 系统上,您需要将 bin 目录中的 phantomjs 可执行文件赋予执行权限 chmod +x /path/to/screen/bin/phantomjs

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

Linux 要求

  • php-bz2 - apt-get install -y php7.2-bz2apt-get install -y php7.4-bz2
  • 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 创建截图。

Fontes - 添加 - 17/02/2021