var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
var url = system.args[1];
var width = system.args[2];
var height = system.args[3];
var outName = system.args[4];
var outNameFile = outName + ".png";
var outNameCode = outName + ".html";
page.viewportSize = { width: width, height: height };
page.clipRect = { top: 0, left: 0, width: width, height: height };
page.open(url, function() {
page.evaluate(function() {
var style = document.createElement('style'),
text = document.createTextNode('body { background: #fff }');
style.setAttribute('type', 'text/css');
style.appendChild(text);
document.head.insertBefore(style, document.head.firstChild);
});
setTimeout(function() {
page.render(outNameFile);
var content = page.content;
fs.write(outNameCode, content, 'w');
phantom.exit();
}, 2000);
});