Ez tulajdonképpen egy parancssoros böngésző, ami a kimenetét képbe nyomja ki. Az, hogy screenshot-ot lehet csinálni egy oldalról, csak egy lehetséges felhasználása. E mellett egy csomó okosságot ki lehet találni rá. Pl. képmanipuláló scripteket írni JS-ben Canvas-al ...
Originally shared by Code-Infection
How can you create a screenshot from an url on server-side?
Just use PhantomJS :)
Sample:
var address, size, width, height, prefix;
function savePage(address, width, height, prefix, cb) {
var page = require('webpage').create();
var name = width + "x" + height;
page.viewportSize = { width: width, height: height };
page.clipRect = { width: width, height: height };
console.log("Load " + address + " with " + name + "...");
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
console.log('Generate ' + name + "..." );
page.render("output/" + prefix + "-" + name + ".png");
cb();
}, 500);
}
});
}
if (phantom.args.length < 3) {
console.log('Usage: webshot.js URL width height [prefix]');
phantom.exit();
} else {
address = phantom.args[0];
width = phantom.args[1];
height = phantom.args[2];
prefix = phantom.args[3] || "page";
savePage(address, width, height, prefix, function() { phantom.exit(); });
}
$ phantomjs webshot.js http://youtube.com/ 1200 800 youtube
Load http://youtube.com/ with 1200x800...
Generate 1200x800...
$ file output/youtube-1200x800.png
output/youtube-1200x800.png: PNG image data, 1200 x 800, 8-bit/color RGBA, non-interlaced
#javascript #phantomjs #screenshot
http://phantomjs.org/
Nincsenek megjegyzések:
Megjegyzés küldése