Skip to content

Instantly share code, notes, and snippets.

@JonathanMH
Forked from spion/gist:3740438
Created September 18, 2012 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanMH/3742525 to your computer and use it in GitHub Desktop.
Save JonathanMH/3742525 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var im = require('imagemagick');
var async = require('async');
var widths = ['270', '590', '910', '1230'];
var source_dir = process.cwd()+'/source/';
var output_dir = process.cwd()+'/output/';
fs.readdir(source_dir, function(err, files){
if (err) throw err;
console.log(files);
var parallelResizes = 1;
async.mapSeries(files, function (file, cb) {
im.identify(source_dir + file, cb);
},
function (err, identifiers) {
var queue = async.queue(function (options, callback) {
im.resize(options, callback);
}, parallelResizes);
identifiers.forEach(function (id, file) {
filename = files[file].split('.');
for(i = 0; i < widths.length; i++){
aspect = id.width / id.height;
width = widths[i];
height = Math.round(widths[i] / aspect);
current = {
srcPath: source_dir + files[file],
width: width,
height: height,
dstPath: output_dir + filename[0] + '_w' + width + '_h' + height + '.' + filename[1],
quality: 1,
progressive: true
};
queue.push(current);
}
});
queue.drain = function () {
console.log("resizing complete");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment