Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using node sharp package to resize image and upload to s3 it is rotated

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
                };
            }
        });
    });
});
like image 234
jaget Avatar asked Mar 12 '16 14:03

jaget


People also ask

How to use sharp to resize images?

The resizeImage() function chains the sharp module's resize() method to the sharp instance. The method takes an object as an argument. In the object, you set the image dimensions you want using the width and height property. Setting the width to 150 and the height to 97 will make the image 150px wide, and 97px tall.

How do I resize an image in node JS?

NodeJS – Resize() is an inbuilt function that is used to resize the images to the desired size. We can use resize to set the height and width using a 2-pass bilinear algorithm. It can resize an image into any size as declared by the user. We can take input from the user or resize it into fixed Width*Height size.

How do I upload images to Amazon S3?

To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

Can images be stored in S3?

You can create a dataset using images stored in an Amazon S3 bucket. With this option, you can use the folder structure in your Amazon S3 bucket to automatically classify your images. You can store the images in the console bucket or another Amazon S3 bucket in your account.


1 Answers

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

like image 125
FastTurtle Avatar answered Oct 06 '22 01:10

FastTurtle