1

I need to resize my images before I upload them to s3 (amazon). I tried this function but it's not working.

Here is the function that uploads the image.

My file name is: beach_life-normal.jpg

I tried this new code but it still doesn't work!!!

This is my code:

var AWS = require('aws-sdk'),
fs = require('fs');
var express = require("express");
var app = express();
var im = require('imagemagick');



// For dev purposes only
AWS.config.update({ accessKeyId: '', secretAccessKey: '' });

// Read in the file, convert it to base64, store to S3
var fileStream = fs.createReadStream('beach_life-normal.jpg');
fileStream.on('error', function (err) {
  if (err) { throw err; }
});  
fileStream.on('open', function () {
  var s3 = new AWS.S3();

im.resize({
  srcPath: 'beach_life-normal.jpg',
  dstPath: 'beach_life-normal-small.jpg',
  width:   256

});

  s3.putObject({
    Bucket: 'adinoauploadefile',
    Key: 'beach_life-normal.jpg',
    Body: fileStream
  }, function (err) {
    if (err) { throw err; }
  });

});
Rafael Barros
  • 2,738
  • 1
  • 21
  • 28
adi
  • 191
  • 2
  • 14
  • In my experience the `imagemagick` module is pretty pointless - it's just a restrictive wrapper around the command line utilities.. See my answer for a more efficient way of doing it directly. – OrangeDog May 11 '16 at 09:57

3 Answers3

0

In your example you're saving the image to S3 (s3.putObject) before resizing it (im.resize). Move the resize function before the put.

You're also not passing the image to the resize function; you'll need something like

im.resize(fileStream, { // pass in the image
      height:100,
      width:   200
 }, function(err, stdout, stderr){
      if (err) throw err;
      console.log('resized image to fit within 200x200px');
 });

Check the docs for the library you're using for the correct syntax.

whostolemyhat
  • 3,107
  • 4
  • 34
  • 50
0

You seem to be lacking basic concepts like "before" and data flow, but I'll try anyway.

You need to insert something between reading the file and uploading to S3. A convert process would seem like a good idea.

var args = ['beach_life-normal.jpg', '-resize', '200x200>', 'JPEG:-']
var convert = child_process.spawn('convert', args);
var s3 = new AWS.S3();

s3.putObject({
    Bucket: 'adinoauploadefile',
    Key: 'beach_life-normal.jpg',
    Body: convert.stdout
},
// ...

I'll leave the error handling, etc. to you.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
0

use this code.

im.convert(['kittens.jpg', '-resize', '25x120', 'kittens-small.jpg'], 
function(err, stdout){
  if (err) throw err;
  console.log('stdout:', stdout);
});

In place of kittens.jpg ,you can give path of image or in your case you can use fileStream and kittens-small.jpg will be name of resized image name.

Manoj Ojha
  • 373
  • 2
  • 11