I am using this node package:
https://www.npmjs.com/package/sharp
I use it to resize an image and then upload it to amazon S3.
Most images are find but some of them (I assume based on aspect ratio) get rotated.
Is there a way to prevent this or a reason for it?
Here is a copy of the code I am using. imageData is either data taken from an s3 bucket file of from file upload. As you can see I am not calling the rotate function. Is there anyway to 'lock' rotation?
module.exports.resize = function(imageData, width, fileName){
sharp(imageData).resize(parseInt(width), null).toBuffer(function (err, data) {
if (err) throw err;
s3.putObject({
Bucket: aws.bucket,
Key: 'images/' + width + '/' + fileName,
Body: data
}, function (err, data) {
if (err) {
console.log('Failed to resize image due to an error: ' + err);
return {
message: 'Failed to resize image due to an error: ' + err
};
} else {
console.log('s3 image uploaded to ' + 'images/' + width + '/' + fileName);
return {
message: 's3 image uploaded to ' + 'images/' + width + '/' + fileName
};
}
});
});
});
Working code to fetch image from url(s3 url) ,resize it and then upload the resized image to s3
var sharp = require('sharp')
var request = require('request').defaults({encoding: null})
var s3Upload = require('./s3Upload')
module.exports = function (fileLocation) {
request(fileLocation, function (error, response, body) {
var fileInstance400x400 = sharp(body)
var inst400x400 = fileInstance400x400.resize(400, 400)
s3Upload('filename400x400', inst400X400)
})
}
// fileLocation iabsolute url of image
s3Upload module
var aws = require('aws-sdk')
var config = require('../config')
/**
* @param fileName: filename to be saved
* @param file: bufferd data
*/
function defaultContentType(req, file, cb) {
setImmediate(function () {
var ct = file.contentType || file.mimetype || 'application/octet-stream'
cb(null, ct);
});
}
module.exports = function (fileName, file) {
aws.config.update({
accessKeyId: config.S3_CONF.access_key_id,
secretAccessKey: config.S3_CONF.access_key,
region: config.S3_CONF.region,
contentType: defaultContentType,
limits: {fileSize: 1000000, files: config.MAX_FILE_COUNT || 6}
})
var s3bucket = new aws.S3({params: {Bucket: config.S3_CONF.media_bucket}});
var params = {Key: fileName, Body: file};
// TODO setting proper header for s3
s3bucket.upload(params, function (err, data) {
if (err) {
console.log('Error uploading data: ', err);
} else {
console.log('Successfully uploaded data to myBucket/myKey');
}
});
}
hope it helps