Skip to content

Commit

Permalink
Add a thumbnail function and make gravity a parameter for resizeAndCrop
Browse files Browse the repository at this point in the history
  • Loading branch information
distracteddev committed May 4, 2014
1 parent 0017c2c commit 42b07fb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
71 changes: 70 additions & 1 deletion lib/imager.js
Expand Up @@ -245,6 +245,20 @@ Imager.prototype = {
});
}

if (variant.thumbnail) {
Object.keys(variant.thumbnail).forEach(function (name) {
var processFiles = function (cb) {
var preset = {
name: name,
size: variant.thumbnail[name],
sep: variant.separator || '_'
};
self.thumbnail(file, preset, filename, cb);
};
asyncArr.push(processFiles);
});
}

async.parallel(asyncArr, function (err, results) {
var f = _.uniq(results).toString();

Expand All @@ -257,6 +271,55 @@ Imager.prototype = {
});
},

/**
* Create a thumbnail scaled and cut to fit
*
* @param {Object} file
* @param {Object} preset
* @param {String} filename
* @param {Function} cb
* @return {Imager}
* @api public
*/

thumbnail: function(file, preset, filename, cb) {
console.log("Calling thumbnail")
var self = this;
var ct = file['type'] || file.headers['content-type'];
var remoteFile = preset.name + preset.sep + filename;
var tempFile = path.join(tempDir, 'imager_' +
Math.round(new Date().getTime()) + '_' +
Math.floor(Math.random() * 1000) + contentType[ct]);

var gravity = 'Center'
if (preset.size.indexOf(' ') > -1) {
gravity = preset.size.split(' ')[1];
preset.size = preset.size.split(' ')[0];
}

var w = preset.size.split('x')[0]
var h = preset.size.split('x')[1]

var cmd = gm(file['path'])
.autoOrient()
.command('convert')
.out('-thumbnail', w+'x'+h+'^')
.gravity(gravity)
.extent(w,h)
.write(tempFile, function (err) {
if (err) return cb(err);
async.each(self.storage, function (storage, cb) {
self['pushTo' + storage](tempFile, remoteFile, filename, ct, cb);
}, function (err) {
fs.unlink(tempFile, function (err) {
if (err) console.error(err);
});
if (err) cb(err);
else cb(null, filename);
});
});
},

/**
* Resize file
*
Expand Down Expand Up @@ -355,10 +418,16 @@ Imager.prototype = {
Math.round(new Date().getTime()) + '_' +
Math.floor(Math.random() * 1000) + contentType[ct]);

var gravity = 'NorthWest'
if (preset.type.crop.indexOf(' ') > -1) {
gravity = preset.type.crop.split(' ')[1];
preset.type.crop = preset.type.crop.split(' ')[0];
}

gm(file['path'])
.autoOrient()
.resize(preset.type.resize.split('x')[0], preset.type.resize.split('x')[1])
.gravity('Center')
.gravity(gravity)
.crop(preset.type.crop.split('x')[0], preset.type.crop.split('x')[1])
.write(tempFile, function (err) {
if (err) return cb(err);
Expand Down
22 changes: 15 additions & 7 deletions test/imager.js
Expand Up @@ -2,16 +2,24 @@
module.exports = {
variants: {
items: {
// keepNames: true,
resize: {
mini : "300x200",
preview: "800x600"
rename: function (filename) {
return Date.now() + '_' + filename;
},
crop: {
thumb: "200x200"
thumbnail: {
mini_thumb_center: "160x160",
medium_thumb_center: "360x360",
mini_thumb: "160x160 NorthWest",
medium_thumb: "360x360 NorthWest"
},
resize: {
original : "100%",
mini : "160x160",
medium : "360x360",
large : "640x640"
},
resizeAndCrop: {
large: {resize: "1000x1000", crop: "900x900"}
// mini_thumb : {crop: "160x160", resize: "480x480"},
// medium_thumb : {crop: "360x360", resize: "1080x1080"}
}
},

Expand Down

0 comments on commit 42b07fb

Please sign in to comment.