Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - How to resize image in Node js

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

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...