我可以使用node-gm调整内存中的图像大小,还是需要先将其保存起来?

我允许用户上传头像,但是我想在上传时调整头像的大小。 现在看起来,gm让我把它保存到磁盘之前,它resize。 当传入的请求进来时是否有可能调整它的大小?

像这样的东西:

var readStream = fs.createReadStream(req.files['profile-picture']['path']); gm(req.files['profile-picture']['path'], 'img.jpg') .write('/path/to/directory/img.jpg', function (err) { if (!err) console.log('done'); }); 

你可以通过gmpipe道stream,而不需要将文件保存到光盘! 你甚至可以一个接一个地执行几个操作,因为gm函数是可链接的(在我的例子中,我调整了图像的大小,后缀从中间裁剪(因此重力函数)

 gm(req.files['profile-picture']['path'], 'img.jpg') .resize("100^", "100^") .gravity('Center') .crop(100, 100) .stream(function (err, stdout, stderr) { // do whatever you want with your output stream (stdout) here });