minicodemonkey / portrayal
Simple, self-contained library allows you to capture screenshots of websites using PhantomJS.
Requires
- php: >=5.4.0
- jakoch/phantomjs-installer: 2.1.1-p05
- symfony/process: ^3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
Portrayal
This simple, self-contained library allows you to capture screenshots using PhantomJS.
Installation
You can install this package through Composer. Edit your project's composer.json
file to require minicodemonkey/portrayal
.
"require": { "minicodemonkey/portrayal": "~2.0" }
You will also need to add post-install and post-update scripts to composer.json
as well as a config
entry to set up the phantomjs
binary dependency:
"config": { "bin-dir": "bin" }, "scripts": { "post-install-cmd": [ "PhantomInstaller\\Installer::installPhantomJS" ], "post-update-cmd": [ "PhantomInstaller\\Installer::installPhantomJS" ] }
Now run composer update
from the terminal, and you're good to go!
For more information, check out jakoch/phantomjs-installer
Usage
$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
Adjust timeout
The time in seconds to wait on the phantomjs process before giving up. The default timeout is 30s
.
$capture = new \Portrayal\Capture; $filename = $capture->setTimeout(10) ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
Adjust rendering delay
The time in seconds to wait taking the screenshot after the webpage has loaded. The default value is 0.35s
(350ms
).
$capture = new \Portrayal\Capture; $filename = $capture->setRenderDelay(200) ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
Disable animations
This will inject a couple of scripts to disable CSS3 animations as well as jQuery animations. Useful for making sure that subsequent screenshots will have the same state.
$capture = new \Portrayal\Capture; $filename = $capture->disableAnimations() ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
Change user agent
This allows you to change the HTTP User-Agent header set by the phantomjs process.
$capture = new \Portrayal\Capture; $filename = $capture->setUserAgent('MyScreenShotApp 1.0') ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
Change browser viewport dimensions
This will change the browser viewport. Please note that this might not necessarily correspond to the exact dimensions of the screenshot. Please see https://github.com/ariya/phantomjs/issues/10619 for additional details.
$capture = new \Portrayal\Capture; $filename = $capture->setViewPort($width = 320, $height = 480) ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());
Specify a custom cookie jar
This will allow you to set specific cookies for the session, this is for example useful for retaining the cookie state across multiple requests.
$capture = new \Portrayal\Capture; $filename = $capture->setCookiesFile('/tmp/cookies.txt') ->snap('https://github.com/minicodemonkey/Portrayal', sys_get_temp_dir());