Last active
March 27, 2016 08:13
-
-
Save Dev-Dipesh/30f7309d013f02dff42b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
// Adds whitespace to image at left and right in case of portrait to make it appear as landscape | |
// Based on FB Image Size for posts shared with app | |
// Make sure to unlink once done. | |
// Uploads Images to S3 | |
uploadImage: function (req, res) { | |
'use strict'; | |
req.files.image = req.files.Filedata || req.files.image || req.files.files[0]; | |
var image = req.files.image; | |
var fb_cross = req.headers['fb_cross'] || '1'; | |
var s3Headers = { | |
'Content-Type': image.type, | |
'x-amz-acl': 'public-read' | |
}; | |
logger.info(image.path); | |
logger.info("FB CROSS:", fb_cross); | |
// Image Size with Aspect Ratio | |
var dimensions = sizeOf(image.path); | |
console.log("Image Size: ", dimensions); | |
var ratio = Math.min(1200 / dimensions.width, 630 / dimensions.height); | |
console.log('RATIO: ', ratio); | |
// This will be new width and height | |
var nWidth = 0; | |
var nHeight = 0; | |
if (dimensions.width * ratio < 1200) { | |
nWidth = 1200; | |
nHeight = 630; | |
} else { | |
nWidth = dimensions.width * ratio; | |
nHeight = dimensions.height * ratio; | |
} | |
var fileName = image.path.split('/').pop(); | |
var aerror; | |
var s3response; | |
// For portraits | |
if (dimensions.width < dimensions.height && fb_cross === '1') { | |
var imgWidth = (dimensions.height - dimensions.width) / 2 - 10; | |
var whiteImg = 'app/public/img/whiteImg' + process.hrtime() + '.jpg'; | |
async.parallel([ | |
function (callback) { | |
gm(imgWidth, dimensions.height, "#ffffff").write(whiteImg, function (err) { | |
callback(); | |
}); | |
}, function (callback) { | |
gm(whiteImg).append(image.path, whiteImg, true).noProfile().resize(nWidth, nHeight).autoOrient().write(image.path, function (error) { | |
callback(); | |
}); | |
}, function (callback) { | |
var fileName = image.path.split('/').pop(); | |
s3.putFile(image.path, fileName, s3Headers, function (error, _s3response) { | |
if (error) { | |
logger.error(error); | |
} | |
s3response = _s3response; | |
callback(); | |
}); | |
} | |
], function () { | |
postModel.createImage(s3response.req.url, image.name, fileName, function (error, imageId) { | |
if (error) { | |
logger.error(error); | |
} | |
fs.readFile(image.path, function (err, file) { | |
res.send({ | |
status: "success", | |
imageId: imageId, | |
url: s3response.req.url, | |
base64: 'data:' + image.type + ';base64,' + file.toString('base64') | |
}); | |
fs.unlink(image.path, function (error) { | |
if (error) { | |
logger.error(error); | |
} | |
}); | |
fs.unlink(whiteImg, function (error) { | |
if (error) { | |
logger.error(error); | |
} | |
}); | |
}); | |
}); | |
}); | |
} else { | |
async.parallel([function (callback) { | |
gm(image.path).noProfile().resize(nWidth, nHeight).autoOrient().write(image.path, function (error) { | |
if (error) { | |
logger.error(error); | |
} | |
callback(); | |
}); | |
}, function (callback) { | |
s3.putFile(image.path, fileName, s3Headers, function (_error, _s3response) { | |
aerror = _error; | |
s3response = _s3response; | |
callback(); | |
}); | |
}], function () { | |
if (aerror) { | |
logger.error(aerror); | |
res.send(); | |
} else { | |
postModel.createImage(s3response.req.url, image.name, fileName, function (error, imageId) { | |
if (error) { | |
logger.error(error); | |
} | |
fs.readFile(image.path, function (err, file) { | |
res.send({ | |
status: "success", | |
imageId: imageId, | |
url: s3response.req.url, | |
base64: 'data:' + image.type + ';base64,' + file.toString('base64') | |
}); | |
fs.unlink(image.path, function (error) { | |
if (error) { | |
logger.error(error); | |
} | |
}); | |
}); | |
}); | |
} | |
}); | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment