Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created April 27, 2015 20:01
Show Gist options
  • Save thiagozs/2a1b9bb2eb7c3a443389 to your computer and use it in GitHub Desktop.
Save thiagozs/2a1b9bb2eb7c3a443389 to your computer and use it in GitHub Desktop.
POC imagemagick for resize and change name of images
var S = require('string');
var moment = require('moment');
var util = require('util');
var im = require('node-imagemagick');
var _ = require("underscore");
var sizes = [{
width: 200,
name: '-small'
}, {
width: 300,
name: '-thumb'
}, {
width: 780,
name: '-post'
}];
//var fileSrc = 'game_original.jpg';
var fileSrc = 'alyson_original.gif';
//var fileSrc = 'beyonce_original.gif';
//var fileSrc = 'lookbook_original.jpg';
_.each(sizes, function(ele) {
im.identify(fileSrc, function(err, output) {
if (err) throw err;
console.log('width : ' + output.width);
if ((/(.*?)(\.\w{3,4})$/).test(fileSrc)) {
var gr = fileSrc.match(/(.*?)(\.\w{3,4})$/);
//var fileDst = gr[1] + ele.name + gr[2];
var fileDst = gr[1] + ele.name + '.jpg';
if (output.width <= 780 && ele.width == 780)
ele.width = output.width;
if (S(gr[2]).contains('gif')) {
im.convert([fileSrc+'[1]', '-resize', ele.width+'x', fileDst], function(err, stdout) {
if (err) throw err;
});
} else {
im.resize({
srcPath: fileSrc,
dstPath: fileDst,
width: ele.width,
quality: 0.6
}, function(err, stdout, stderr) {
if (err) throw err;
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment