Last active
August 29, 2015 13:56
-
-
Save memorycraft/8925088 to your computer and use it in GitHub Desktop.
gm:イメージ構成のメイン処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//モジュール読込 | |
var http = require('http'); | |
var url = require('url'); | |
var gm = require('gm'); | |
var gmcomposite = require('./gmcomposite'); | |
var fs = require('fs'); | |
var argv = require('argv'); | |
//コマンドオプション定義 | |
argv.option([{ | |
name: 'facebook_id', | |
short: 'f', | |
type : 'string', | |
description :'your facebook_id', | |
example: "'script --facebook_id=value' or 'script -f value'" | |
}, | |
{ | |
name: 'url', | |
short: 'u', | |
type: 'string', | |
description: 'your picture url', | |
example: "'script --url=value or 'script -u value''" | |
}]); | |
var config = argv.run(); | |
//画像パス定義 | |
var base_path = './assets/frame.png'; | |
var mask_path = './assets/frame.png'; | |
var down_path = './rslt/download/'+config.options.facebook_id+".jpg"; | |
var convert_path = './rslt/convert/'+config.options.facebook_id+".jpg"; | |
var composite_path = './rslt/composite/'+config.options.facebook_id+'.jpg'; | |
var target_url = url.parse(config.options.url); | |
target_url.protocol = 'http:'; | |
target_url.path = target_url.pathname; | |
config.base_path = base_path; | |
config.mask_path = mask_path; | |
config.down_path = down_path; | |
config.convert_path = convert_path; | |
config.composite_path = composite_path; | |
/** | |
* URLの画像を指定ディレクトリにダウンロード | |
* @param {string} target_url:ダウンロード対象のURL | |
* @param {string} save_path:保存先 | |
* @param {string} fn:コールバック関数 | |
*/ | |
function get(target_url, save_path, fn) { | |
var callee = arguments.callee; | |
var opts = url.parse(target_url); | |
var req = (opts.protocol.match(/https/) ? https : http).request({ | |
host: opts.hostname, | |
port: opts.port, | |
path: opts.pathname + (opts.search || ''), | |
method: 'GET', | |
}); | |
req.on('response', function(res){ | |
if(res.statusCode == 301 ||res.statusCode == 302) { | |
callee(res.headers.location, destPath, fn); | |
} else if(res.statusCode == 200) { | |
var writableStream = fs.createWriteStream(save_path); | |
writableStream.on('error', fn); | |
writableStream.on('close', fn); | |
res.on('data', function(chunk){ | |
writableStream.write(chunk, 'binary'); | |
}); | |
res.on('end', function(){ | |
writableStream.end(); | |
}); | |
} else { | |
fn(new Error('statusCode is ' + res.statusCode)); | |
} | |
}); | |
req.on('error', fn); | |
req.end(); | |
} | |
/** | |
* ダウンロードした画像をマスク合成 | |
*/ | |
get(target_url, down_path, function(err){ | |
//エラーなら出力して終了 | |
if(err){console.log(err);return;} | |
//画像処理 | |
gm(down_path) | |
.extent(800,800)//リサイズ | |
.quality(100)//画質 | |
.write(convert_path, (function(data){ return function(err){//写真1次加工 | |
//エラーなら終了 | |
if(err){console.log(err);return;} | |
//合成処理 | |
gmcomposite(data.base_path, data.convert_path, data.mask_path, data.composite_path, (function(data){ return function(err){ | |
if(err){console.log(err);return;} | |
console.log("create complete !"); | |
}})(data)) | |
}})(config)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment