我允许用户上传头像,但我想在上传时调整它们的大小。现在,gm似乎让我先将文件保存到磁盘,然后再调整大小。当传入的请求进来时,有没有可能只是调整它的大小?
如下所示:
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');
});发布于 2013-06-19 18:00:02
您只需通过gm管道传输一个流,而无需将文件保存到光盘!您甚至可以一个接一个地执行几个操作,因为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
});https://stackoverflow.com/questions/17186522
复制相似问题