PHANTOMJS (http://phantomjs.org/) is the WebKit of a server-side JavaScript API. It supports a variety of Web standards: DOM processing, CSS selectors, JSON, Canvas, and SVG, can be easily applied to a variety of automated testing, screen capture, network monitoring and other environments.
Windows version of the PHANTOMJS installation is relatively simple, the following is a Linux version of the PHANTOMJS installation and simple usage.
There are two ways to install Linux version, one is to download the corresponding system compiled executable file use, but may appear unavailable, then need to use the second, source code compiled installation.
First, directly download the compiled file
Direct to bitbucket download the corresponding compression package (link: https://bitbucket.org/ariya/phantomjs/downloads), for example, my CentOS6.5 64-bit system, Download is phantomjs-1.9.8-linux-x86_64.tar.bz2, download completed after decompression, in the bin directory has compiled a good phantomjs file, directly with FTP and other tools uploaded to the server.
Use SSH login server, to Phantomjs Plus execute permissions chmod +x PHANTOMJS, run./phantomjs-h See if you can display the Help document, the error may need to use the source code compiled to install.
Second, the source code compilation
First go to the official website download the latest stable version of the source code (link: http://phantomjs.org/download.html), to find Linux under the source code download, the latest version is 2.0 direct download
The following actions take CentOS as an example, and other distributions are similar
Cd/usr/local/src
# download Source
wget Https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.0.0-source.zip
# decompression source, no unzip command, please use yum install unzip installation
Unzip Phantomjs-2.0.0-source.zip
# Install the compilation environment
Yum-y install GCC gcc-c++ make flex bison gperf ruby \
Openssl-devel freetype-devel fontconfig-devel libicu-devel sqlite-devel \
Libpng-devel Libjpeg-devel
# Start compiling
CD phantomjs-2.0.0
./build.sh
The compilation operation takes longer (can be more than half an hour) and waits patiently for completion. After the completion of the compilation will be a bin directory, which has compiled the completed PHANTOMJS files.
Simple to use
If the source is downloaded, there will be a example folder, we use the rasterize.js to create a screenshot of the site. The downloaded code can be copied and saved as rasterize.js without this file.
var page = require (' webpage '). Create (),
System = require (' System '),
Address, output, size;
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" ");
Console.log (' image (Png/jpg output) Examples: "1920px" entire page, window width 1920px ');
Console.log (' 800px*600px ' window, clipped to 800x600 ');
Phantom.exit (1);
} else {
address = system.args[1];
output = system.args[2];
Page.viewportsize = {width:600, height:600};
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 '};
else if (System.args.length > 3 && system.args[3].substr ( -2) = = "px") {
Size = System.args[3].split (' * ');
if (size.length = = 2) {
PageWidth = parseint (size[0], 10);
PageHeight = parseint (size[1], 10);
Page.viewportsize = {width:pagewidth, height:pageheight};
Page.cliprect = {top:0, left:0, Width:pagewidth, height:pageheight};
} else {
Console.log ("Size:", system.args[3]);
PageWidth = parseint (system.args[3], 10);
PageHeight = parseint (PageWidth * 3/4, 10); It ' s as good an assumption as any
Console.log ("PageHeight:", pageheight);
Page.viewportsize = {width:pagewidth, height:pageheight};
}
}
if (System.args.length > 4) {
Page.zoomfactor = system.args[4];
}
Page.open (address, function (status) {
if (Status!== ' success ') {
Console.log (' Unable to load the address! ');
Phantom.exit (1);
} else {
Window.settimeout (function () {
Page.render (output);
Phantom.exit ();
}, 200);
}
});
}
Use the following command to create a screenshot of the site (note the file path, please modify your phantomjs and rasterize.js paths yourself)
1
./phantomjs. /example/rasterize.js http://blog.icewingcc.com./icewingcc.png
Phantomjs can also be used for network monitoring, automation and other functions, and the content of JS files can also be modified according to their own needs. Please refer to the official website and instructions for the example in the example folder for specific use.