Skip to content

Instantly share code, notes, and snippets.

@marcelduran
Created May 14, 2014 18:09
Show Gist options
  • Save marcelduran/1e91b64849e4c0572de2 to your computer and use it in GitHub Desktop.
Save marcelduran/1e91b64849e4c0572de2 to your computer and use it in GitHub Desktop.
PhantomJS script to pseudo-retina screenshots
var page = require('webpage').create(),
address, output, size;
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg);
};
if (phantom.args.length < 2 || phantom.args.length > 6) {
console.log('Usage: rasterize.js URL filename WxH retina ua');
phantom.exit();
} else {
address = phantom.args[0];
output = phantom.args[1];
size = /(\d+)x(\d+)/i.exec(phantom.args[2]) || [];
page.viewportSize = { width: parseInt(size[1], 10) || 1280,
height: parseInt(size[2], 10) || 1024 };
phantom.args[4] && (page.settings.userAgent = phantom.args[4]);
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
page.evaluate(function (retina) {
console.log(retina);
if (retina) {
/* scale the whole body */
document.body.style.webkitTransform = "scale(2)";
document.body.style.webkitTransformOrigin = "0% 0%";
/* fix the body width that overflows out of the viewport */
document.body.style.width = "50%";
}
}, /(?:1|true|yes)/i.test(phantom.args[3]));
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}
@christianhaller
Copy link

Save this gist as rasterize.js and call it like this:
phantomjs rasterize.js http://www.stern.de homepage-stern-de.png 2000x18000 true ua

If your webseite has webfonts use phantomjs in Version 2 or slimerjs (with Moz prefix instead of webkit)

@Veejay
Copy link

Veejay commented Nov 18, 2016

Note that phantom.args has been removed, in favor of system.args.

Add

var system = require('system');
var args = system.args

at the top of the file and then replace all subsequent calls to phantom.args with args (with rasterize.js being args[0])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment