Skip to content

Instantly share code, notes, and snippets.

@radmen
Created May 23, 2012 21:21
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 radmen/2777914 to your computer and use it in GitHub Desktop.
Save radmen/2777914 to your computer and use it in GitHub Desktop.
PhantomJS Shotr - simple script to make webpage shot
var page = require('webpage').create(),
system = require('system'),
fs = require('fs'),
uri = document.createElement('a'), // from this https://gist.github.com/2428561
viewport,
hash = function(string) {
var hash = 0,
char;
if (string.length == 0) return hash;
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
};
if(system.args.length < 2) {
console.log('Usage: phantomjs ' + system.args[0] + ' url [viewport]');
phantom.exit();
}
page.viewportSize = {
'width': 1024,
'height': 728
};
if(system.args[2]) {
viewport = system.args[2].split('x');
if(!isNaN(viewport[0]) && !isNaN(viewport[1])) {
page.viewportSize = {
'width': viewport[0],
'height': viewport[1]
};
}
}
uri.href = system.args[1];
if('file:' == uri.protocol) {
console.log('Enter valid URL');
phantom.exit();
}
page.open(system.args[1], function(status) {
var dir = uri.hostname,
filename = hash(uri.pathname + uri.search) + '.jpg',
full_path = dir + fs.separator + filename;
if(!fs.exists(dir) || !fs.isDirectory(dir)) {
fs.makeDirectory(dir);
}
page.render(full_path);
console.log(fs.workingDirectory + fs.separator + full_path);
phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment