Resize image in node js

最后都变了- 提交于 2020-06-27 17:59:11

问题


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

});

回答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; }
    });
  });  
});


来源:https://stackoverflow.com/questions/37146106/resize-image-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!