Skip to content

Instantly share code, notes, and snippets.

@macmladen
Created September 18, 2015 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macmladen/8387aee25d6ed1873f5a to your computer and use it in GitHub Desktop.
Save macmladen/8387aee25d6ed1873f5a to your computer and use it in GitHub Desktop.
Script to capture screen using phantomjs.org headless
// Copyright: (C) 2015 MacMladen <macmladen@gmail.com>
// 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