Skip to content

Instantly share code, notes, and snippets.

@whatvn
Last active August 29, 2015 14:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save whatvn/9e5c07ba0a24bf5151e6 to your computer and use it in GitHub Desktop.
script based on rasterize.js (phantomjs) to take screenshot of a given website (not full page), code listed here I just copy from many website and combined into working one.
yum install -y xorg-x11-server-Xvfb xorg-x11-server-Xorg xorg-x11-fonts* dbus-x11 xulrunner.x86_64 nspr.x86_64 nss.x86_64
yum install -y flash-plugin nspluginwrapper libcurl
rpm -ivh http://www.botsphere.com/rpms/phantomjs-1.9-1.x86_64.rpm
var page = require('webpage').create(),
system = require('system'),
address, output, size;
var width = 1280;
var height = 800;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: width, height: height };
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
size = system.args[3].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
: { format: system.args[3], orientation: 'portrait', margin: '1cm' };
}
if (system.args.length > 4) {
page.zoomFactor = system.args[4];
}
page.onInitialized = function () {
page.evaluate(function () {
window.navigator = {
plugins: {length: 2, 'Shockwave Flash': {name: 'Shockwave Flash', description: 'Shockwave Flash 11.6 r602'}},
mimeTypes: {length: 2, "application/x-shockwave-flash":
{description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash", enabledPlugin: {description: "Shockwave Flash 11.6 r602"}}
},
appCodeName: "Mozilla",
appName: "Netscape",
appVersion: "5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22",
cookieEnabled: true,
language: "en",
onLine: true,
platform: "CentOS 5.7",
product: "Gecko",
productSub: "20030107",
userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22",
};
});
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
page.evaluate(function(w, h) {
document.body.style.width = w + "px";
document.body.style.height = h + "px";
}, width, height);
page.clipRect = {bottom: 0, left: 0, width: width, height: height}; page.render(output);
phantom.exit();
}, 500);
}
});
}
export QTWEBKIT_PLUGIN_PATH=/usr/lib/browser-plugins:/usr/lib/flash-plugin:/usr/lib/mozilla/plugin:/usr/lib64/flash-plugin:/usr/lib64/mozilla/plugins:/usr/lib64/opera/plugins; export LIBXCB_ALLOW_SLOPPY_LOCK=1;xvfb-run --server-args="-screen 0, 1280x800x24" phantomjs --load-plugins=yes ambientscreenshot.js http://google.com /data/imgs/google.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment