Getting a snapshot of a Web page and generating thumbnails can be done in two steps:
1, get a snapshot of the Web page
2. Generate thumbnail image
Get a snapshot of a Web page
Here we use PHANTOMJS to achieve. For detailed usage of PHANTOMJS, refer to the official website. http://phantomjs.org/
1, installation
My environment is CentOS6.5, install the direct download tarball then decompression can be.
Copy Code code as follows:
# wget HTTPS://BITBUCKET.ORG/ARIYA/PHANTOMJS/DOWNLOADS/PHANTOMJS-1.9.8-LINUX-I686.TAR.BZ2
# TAR-JXVF PHANTOMJS-1.9.8-LINUX-I686.TAR.BZ2
# CP Phantomjs-1.9.8-linux-i686/bin/phantomjs/bin/phantomjs
The PHANTOMJS binaries in the bin directory after decompression in the second step are callable commands.
The third step is to not have to enter the full path of the command when calling the command later.
2. Call
The PHANTOMJS call requires a JS script. This JS script receives two parameters, namely URL and snapshot file name filename, script snap.js content as follows:
Copy Code code as follows:
/*
* desc:get snapshot from URL
* Example:phantomjs snap.js http://www.baidu.com baidu.png
*/
var page = require (' webpage '). Create ();
var args = require (' system '). Args;
var Pagew = 1024;
var pageh = 768;
Page.viewportsize = {
Width:pagew,
Height:pageh
};
var url = args[1];
var filename = args[2];
Page.open (URL, function (status) {
if (Status!== ' success ') {
Console.log (' Unable to load ' + URL + '! ');
Phantom.exit ();
} else {
Window.settimeout (function () {
Page.cliprect = {left:0, top:0, Width:pagew, Height:pageh};
Page.render (filename);
Console.log (' Finish: ', filename);
Phantom.exit ();
}, 1000);
}
});
In this script, there is a small setting, that is, set the browser viewable area of the open page size is 1024*768, and then take the first screen content.
The calling command is as follows:
Copy Code code as follows:
Phantomjs snap.js http://www.baidu.com Baidu.png
Note: The user who executes the command here needs to have write permission to the directory.
3, the effect
Get the screenshot below:
Generate thumbnails
Generate thumbnails with the ImageMagick tool, ImageMagick is a very powerful image processing tools, can transform the image (format conversion, scaling, shearing, blur, inversion, etc.), screen capture, picture display, detailed usage can refer to my ImageMagick use experience A article.
1, installation
The Redhat series can be installed using Yum:
Copy Code code as follows:
# yum Install ImageMagick Imagemagick-devel
Other platform installation please refer to the official website: http://www.imagemagick.org/script/binary-releases.php, according to your system select the appropriate package or compile their own source code.
2. Call
We only use the picture scaling tool here, the syntax is:
Copy Code code as follows:
Convert-resize 320x240 baidu.png Baidu_thumbnail.png
The default is scaling by ratio, and if you want to force scaling, you can add an exclamation point behind the dimension:
Copy Code code as follows:
Convert-resize 320x240! Baidu.png Baidu_thumbnail.png
3, the effect
The resulting thumbnails are as follows:
Consolidation Scripts
If you want to automate the last two steps, you can write a shell script implementation:
Copy Code code as follows:
#!/bin/bash
# desc:create snapshot from URL
# example:sh createsnap.sh http://www.baidu.com Baidu
Url=$1
Image_name=$2
Snapshot_name= "${image_name}.png"
Thumbnail_name= "${image_name}_thumbnail.png"
Phantomjs snap.js $URL $SNAPSHOT _name
Convert-resize 320x240 $SNAPSHOT _name $THUMBNAIL _name
Exit 0