Resize image in node js
Asked Answered
C

1

2

I want to resize my images before I upload them to s3 (amazon). I try to use 'resizeImg' function but its dosent work the image upload in standart size, and not in the new size. my code write in node js and then upload to s3-amazon. image name is:beach_life-normal.jpg

My code:

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

const resizeImg = require('resize-img');

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

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();

resizeImg(fs.readFileSync('beach_life-normal.jpg'), {width: 128, height:  128}).then(buf => {
    fs.writeFileSync('beach_life-normal-new.jpg', buf);
});


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

});
Cully answered 10/5, 2016 at 18:18 Comment(3)
Why are you passing the original file and expecting it to upload the new file?Vicinal
Also, why are you uploading the file before the resize finishes?Vicinal
ok, what I need to do?Cully
I
1

You should upload the new file

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

const resizeImg = require('resize-img');

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

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();

resizeImg(fs.readFileSync('beach_life-normal.jpg'), {width: 128, height:  128}).then(buf => {
    fs.writeFileSync('beach_life-normal-new.jpg', buf);
    s3.putObject({
      Bucket: 'adinoauploadefile',
      Key: 'beach_life-normal-new.jpg',
      Body: 'beach_life-normal-new.jpg'
    }, function (err) {
     if (err) { throw err; }
    });
  });  
});
Iberian answered 10/5, 2016 at 19:25 Comment(3)
I tried this code its upload the new image (=new size) but when I open the new image its said : "cant open this image" , ie dosent upload the image correctCully
are you able to open the image locally? are you sure that the issue is not with the resizing? can you check if the MD5 sum of the local file is identical to the uploaded file?Iberian
yes, I tried to open the image locally and I saw the imageCully

© 2022 - 2024 — McMap. All rights reserved.