Skip to content

Instantly share code, notes, and snippets.

@Kienz
Last active August 29, 2015 14:03
Show Gist options
  • Save Kienz/5221316888752c4b11df to your computer and use it in GitHub Desktop.
Save Kienz/5221316888752c4b11df to your computer and use it in GitHub Desktop.
node.js imagemagick resize
(function() {
'use strict';
var async = require('async'),
fs = require('fs'),
im = require('imagemagick'),
maxworkers = require('os').cpus().length,
path = require('path');
exports.resize = resize;
function resize(params) {
var queue = async.queue(resizeimg, maxworkers);
fs.readdir(params.src, function(err, files) {
files.forEach(function(file) {
console.log('resize: ', file);
queue.push({
src: path.join(params.src, '/', file),
dest: path.join(params.dest, '/', file),
width: params.width,
height: params.height
});
});
});
}
function resizeimg(params, cb) {
var imoptions = {
srcPath: params.src,
dstPath: params.dest
};
if (typeof params.width !== 'undefined') {
imoptions.width = params.width;
}
if (typeof params.height !== 'undefined') {
imoptions.height = params.height;
}
im.resize(imoptions, cb);
}
}());
@Kienz
Copy link
Author

Kienz commented Jul 3, 2014

Example:

var imgResize = require('my_modules/image-resize');

imgResize.resize({
  src: 'img',
  dest: 'img/dist',
  width: 32
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment