Created
May 14, 2014 18:09
-
-
Save marcelduran/1e91b64849e4c0572de2 to your computer and use it in GitHub Desktop.
PhantomJS script to pseudo-retina screenshots
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
| 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); | |
| } | |
| }); | |
| } |
Note that phantom.args has been removed, in favor of system.args.
Add
var system = require('system');
var args = system.argsat 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
Save this gist as
rasterize.jsand call it like this:phantomjs rasterize.js http://www.stern.de homepage-stern-de.png 2000x18000 true uaIf your webseite has webfonts use
phantomjsin Version 2 orslimerjs(with Moz prefix instead of webkit)