How to resize image in Node js

前端 未结 3 1192
灰色年华
灰色年华 2020-12-20 08:10

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

相关标签:
3条回答
  • 2020-12-20 08:32

    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.

    0 讨论(0)
  • 2020-12-20 08:34

    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.

    0 讨论(0)
  • 2020-12-20 08:50

    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.

    0 讨论(0)
提交回复
热议问题