# 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
/*
* 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);
}
});
生成缩略图用的是ImageMagick工具,ImageMagick是个非常强大的图片处理工具,可对图片进行转化(格式转换、缩放、剪切、模糊、反转等)、屏幕截图、图片显示等,详细用法可参考 我的ImageMagick使用心得 一文。
# !/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