Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Resize Image

Tags:

node.js

I cannot resize image. I am using multer for getting files, but how can I resize this image? I tried imagestick but fs didn't save them.

  router.post('/uploadAvatar', 
    multer({
      dest: './public/uploads/images/avatars',
      rename: function (fieldname, filename) {
        return 'avatar'+Date.now();
      }
    }), function(req, res) {
      // resize image
      res.json(newPath);
  });
like image 222
user256968 Avatar asked May 03 '26 04:05

user256968


1 Answers

I like Sharp module to play with images in Node.js:

   router.post('/uploadAvatar', 
    multer({
      dest: './public/uploads/images/avatars',
      rename: function (fieldname, filename) {
        return 'avatar'+Date.now();
      }
    }), function(req, res) {
      // resize image
      sharp(newPath).resize(300, 200).toFile(newPath, function(err) {
         if (err) {
           throw err;
         }
         // output.jpg is a 300 pixels wide and 200 pixels high image
         // containing a scaled and cropped version of input.jpg
         res.json(newPath);
      });
  });
like image 83
michelem Avatar answered May 04 '26 20:05

michelem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!