Shell script gets a snapshot of a webpage and generates a thumbnail image

Source: Internet
Author: User
Tags imagemagick

Getting a snapshot of a webpage and generating thumbnails can be done in two steps:

1. Get a snapshot of a webpage

2. Generate thumbnail images

Get a snapshot of a webpage

Here we use PHANTOMJS to achieve. You can refer to the official website for detailed usage of PHANTOMJS.

1. Installation

My environment is CentOS6.5, install directly download tarball then unzip can.

# 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

In the second step, the Phantomjs binary in the bin directory after decompression is the callable command.

The third step is to not have to enter the command full path at a later time to invoke the command.

2. Call

The PHANTOMJS call requires a JS script. This JS script receives two parameters, namely URL url and snapshot file name filename, script snap.js content as follows:

/* * desc:get snapshot from URL * Author: Ten years later Brother Lou (http://www.cnblogs.com/lurenjiashuo/) * EXAMPLE:PHANTOMJS snap.js http ://www.baidu.com baidu.png */var page = require (' webpage '). Create (); var args = require (' system '). Args;var Pagew = 1024;va R Pageh = 768;page.viewportsize = {  Width:pagew,  height:pageh};var url =  args[1];var filename = args[2];p age . 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 ();}    );

In this script there is also a small setting, that is, to set the open page of the browser viewable area of the size of 1024x768, and then take the first screen content.

The Invoke command is as follows:

Phantomjs snap.js http://www.baidu.com Baidu.png

Note: The user who executes the command here needs to have write access to the directory.

3. Effect

Get the following:

Generate thumbnail images

Generate thumbnails with the ImageMagick tool, ImageMagick is a very powerful image processing tool to convert images (format conversion, zoom, cut, blur, invert, etc.), screen, picture display, detailed usage can refer to my ImageMagick use of the article.

1. Installation

The Redhat series can be installed with Yum:

# 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 to choose the appropriate package or compile their own source code.

2. Call

We only use the Image Zoom tool here, the syntax is:

Convert-resize Baidu.png Baidu_thumbnail.png
3. Effect

The resulting thumbnail image is as follows:

    • Related articles recommended:
    • Linux shell scripts enable FTP to automatically upload backup files
    • Input and output redirection for Linux shell programming
    • How to pass a variable to awk in a shell script
    • This article from: Hobby Linux Technology Network
    • This article link: http://www.ahlinux.com/shell/9138.html

Shell script gets a snapshot of a webpage and generates a thumbnail image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.