I'm allowing users to upload avatars, but I want to resize them as they are uploaded. Right now, it appears that gm makes me save the file to disk first before it resizes it. Is it possible to just have it resize it when the incoming request comes in?
Something like this:
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');
});
you can just pipe a stream through gm, and dont need to save the file to disc! you can even do several operations one after the other because gm-functions are chainable (in my example i resize the image and afterwords crop it from the middle (therefore the gravity function)
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
});