aki/yii2-wbsite-screenshot

yii2 web应用程序的wbsite-screenshot

安装: 231

依赖: 0

建议者: 0

安全: 0

星星: 3

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

1.0.1 2018-03-28 08:19 UTC

This package is auto-updated.

Last update: 2024-09-13 10:58:59 UTC


README

#屏幕

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

安装

通过Composer

php composer.phar require --prefer-dist aki/yii2-wbsite-screenshot:dev-master

或添加

"aki/yii2-wbsite-screenshot": "dev-master"

如果是在任何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 aki\screenshot\ScreenShot;

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

$screenCapture = new ScreenShot($url);
// or
$screenCapture = new ScreenShot();
$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)。有关更多信息,请参阅许可证文件