Permalink
Browse files

add resize and crop option

  • Loading branch information...
1 parent b0c1509 commit fcc16046dc5ac0cfb98cef0da91c374544f00af0 @madhums committed Sep 8, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 lib/imager.js
View
@@ -100,6 +100,15 @@ Imager.prototype.startUpload = function (file, filename, variant, iteratorCallba
})
}
+ if (variants.resizeAndCrop) {
+ Object.keys(variants.resizeAndCrop).forEach(function (name) {
+ var processFiles = function (cb) {
+ self.resizeAndCropFile(file, { name: name, type: variants.resizeAndCrop[name] }, filename, cb)
+ }
+ asyncArr.push(processFiles)
+ })
+ }
+
async.parallel(asyncArr, function (err, results) {
if (err) console.log(err)
var f = _.uniq(results).toString()
@@ -151,6 +160,27 @@ Imager.prototype.cropFile = function (file, preset, fname, cb) {
}
+Imager.prototype.resizeAndCropFile = function (file, preset, fname, cb) {
+ var ct = { 'image/jpeg': '.jpg', 'image/png': '.png', 'image/gif': '.gif' }
+ , fileType = ct[file['type']]
+ , filename = fname + fileType
+ , self = this
+
+ var remoteFile = preset.name + '_' + filename
+ , dest = '/tmp/' + remoteFile
+
+ gm(file['path'])
+ .resize(preset.type.resize.split('x')[0], preset.type.resize.split('x')[1])
+ .gravity('Center')
+ .crop(preset.type.crop.split('x')[0], preset.type.crop.split('x')[1])
+ .write(dest, function(err) {
+ if (err) return cb(err)
+ self['pushTo' + self.storage](dest, remoteFile, filename, file['type'], cb)
+ }
+ )
+}
+
+
Imager.prototype.pushToRackspace = function (file, remoteFile, filename, type, cb) {
var self = this
, client = cloudfiles.createClient(this.config['storage'][this.storage])

0 comments on commit fcc1604

Please sign in to comment.