Created
September 18, 2015 12:30
-
-
Save macmladen/8387aee25d6ed1873f5a to your computer and use it in GitHub Desktop.
Script to capture screen using phantomjs.org headless
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright: (C) 2015 MacMladen <[email protected]> | |
// License: GPL-2.0 | |
// Prequisite: | |
// having installed phantomjs from http://phantomjs.org | |
// You have to change variables to suit your needs | |
// Usage: | |
// $ phantomsj script.js | |
// it will produce capture file in same folder | |
// you can figure out the rest ;) | |
var | |
page = require('webpage').create(), | |
url = 'http://apple.com', // <- change to your url | |
screenFile = 'screenshot.png', // <- change to desired filename | |
screenWidth = 320, // width of screen capture | |
screenHeight = 0, // height is important only if screen is clipped, otherwise is full height (kinda) | |
screenTop = 0, // if you choose to capture some other part | |
screenLeft = 0, // ditto | |
screenZoom = 1; // zooming | |
//viewportSize being the actual size of the headless browser | |
page.viewportSize = { width: screenWidth, height: screenHeight }; | |
//the clipRect is the portion of the page you are taking a screenshot of | |
if (screenHeight) { | |
page.clipRect = { top: 0, left: 0, width: screenWidth, height: screenHeight }; | |
} | |
if (screenZoom) { | |
page.zoomFactor = screenZoom; | |
} | |
page.open(url, function (status) { | |
if (status !== 'success') { | |
console.log('Unable to load the address!'); | |
phantom.exit(1); | |
} else { | |
window.setTimeout(function () { | |
page.render(screenFile); | |
phantom.exit(); | |
}, 200); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment