Created
June 9, 2015 19:07
-
-
Save Echooff3/f7d9baad59e761e552eb to your computer and use it in GitHub Desktop.
Batch Resize & Append with node
This file contains hidden or 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
{ | |
"name": "resizer", | |
"version": "1.0.0", | |
"description": "", | |
"main": "resizer.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"glob": "^5.0.10", | |
"gm": "^1.18.1", | |
"imagemagick": "^0.1.3", | |
"minimist": "^1.1.1", | |
} | |
} |
This file contains hidden or 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
//@ sourceURL=resizer.js | |
var glob = require('glob') | |
var argv = require('minimist')(process.argv.slice(2)); | |
var fs = require('fs'); | |
var gm = require('gm'); | |
var imagehandler = function (err) { | |
if (err) return console.dir(arguments) | |
//console.log(this.outname + ' created :: ' + arguments[3]) | |
process.stdout.write("Remaining Images " + files.length + " Current File: " + this.outname + " \r"); | |
nextStep(undefined) | |
}; | |
var w = parseInt(argv.w); | |
var h = parseInt(argv.h); | |
var ext = 'png'; | |
var outName = __dirname + "/imgs/"+ argv.i + "-" + w + "x" +h+ "-v."+ext | |
var files = []; | |
// options is optional | |
glob(argv.f + "/*.jpg", undefined, function (er, files) { | |
nextStep(files); | |
}); | |
var nextStep = function (f) { | |
if(f !== undefined) { | |
files = f; | |
gm(f.pop()) | |
.resize(null, h) | |
.gravity("center") | |
.crop(w,h) | |
.stream(ext, function (err, stdout, stderr) { | |
if(err) return console.dir(arguments); | |
var ws = fs.createWriteStream(outName); | |
stdout.pipe(ws); | |
nextStep(undefined); | |
}); | |
return; | |
} else { | |
if(files.length == 0) { | |
process.stdout.write(" \n\r"); | |
return; | |
} | |
var n = files.pop(); | |
gm(n) | |
.resize(null, h) | |
.gravity("center") | |
.crop(w,h) | |
.stream(ext, function (err, stdout, stderr) { | |
if(err) return console.dir(arguments); | |
gm(stdout).append(outName) | |
.write(outName, imagehandler); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment