minicodemonkey / portrayal
此包最新版本(v2.2.0)没有提供许可证信息。
简单自包含库,允许您使用 PhantomJS 捕获网站截图。
v2.2.0
2016-11-27 22:50 UTC
Requires
- php: >=5.4.0
- jakoch/phantomjs-installer: 2.1.1-p05
- symfony/process: ^3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
Portrayal
此简单自包含库允许您使用 PhantomJS 捕获截图。
安装
您可以通过 Composer 安装此包。编辑您的项目 composer.json
文件,以要求 minicodemonkey/portrayal
。
"require": { "minicodemonkey/portrayal": "~2.0" }
您还需要在 composer.json
中添加 post-install 和 post-update 脚本,以及一个 config
条目来设置 phantomjs
二进制依赖项。
"config": { "bin-dir": "bin" }, "scripts": { "post-install-cmd": [ "PhantomInstaller\\Installer::installPhantomJS" ], "post-update-cmd": [ "PhantomInstaller\\Installer::installPhantomJS" ] }
现在从终端运行 composer update
,您就可以开始了!
更多信息,请查看 jakoch/phantomjs-installer
使用
$capture = new \Portrayal\Capture; $filename = $capture->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir()); // $filename = /var/folders/6_/htvcfzcd4cb_w9z6bgpmnx5h0000gn/T/d0582362c2ffbf50ee119e504bb64fdc6bba5abd.png
调整超时时间
在放弃之前等待的秒数。默认超时为 30s
。
$capture = new \Portrayal\Capture; $filename = $capture->setTimeout(10) ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
调整渲染延迟
网页加载后等待截图的秒数。默认值为 0.35s
(350ms
)。
$capture = new \Portrayal\Capture; $filename = $capture->setRenderDelay(200) ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
禁用动画
这将注入一些脚本以禁用 CSS3 动画以及 jQuery 动画。这对于确保后续截图具有相同状态很有用。
$capture = new \Portrayal\Capture; $filename = $capture->disableAnimations() ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
更改用户代理
这允许您更改由 phantomjs 进程设置的 HTTP User-Agent 头。
$capture = new \Portrayal\Capture; $filename = $capture->setUserAgent('MyScreenShotApp 1.0') ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
更改浏览器视口尺寸
这将更改浏览器视口。请注意,这不一定对应于截图的确切尺寸。请参阅 https://github.com/ariya/phantomjs/issues/10619 以获取更多信息。
$capture = new \Portrayal\Capture; $filename = $capture->setViewPort($width = 320, $height = 480) ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
指定自定义 cookie jar
这允许您为会话设置特定 cookie,这在例如跨多个请求保留 cookie 状态时很有用。
$capture = new \Portrayal\Capture; $filename = $capture->setCookiesFile('/tmp/cookies.txt') ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());