Node.js 带Express.js的Multer和ImageMagick:上传、调整大小、重新定向

Node.js 带Express.js的Multer和ImageMagick:上传、调整大小、重新定向,node.js,image,express,imagemagick,multer,Node.js,Image,Express,Imagemagick,Multer,我有一个Express应用程序,它通过表单从用户那里获取图像。我需要对图像做几件事,随着图像变得越来越复杂,我不知道如何处理它。这是一个留言板帖子,其中有一些必需的文本字段和可选的图像上传。我需要: 从EXIF数据中找到图像的方向,并根据需要重新确定方向 将原始映像的副本保存到服务器(完成) 创建图像的缩略图并将其保存到服务器(完成) 将记录保存到数据库,无论是否有上载的图像(完成) 我关心我做事的顺序,想知道是否有更有效的方法。我知道我可以在路由内调用upload,而不是将其传入,但我不想在将

我有一个Express应用程序,它通过表单从用户那里获取图像。我需要对图像做几件事,随着图像变得越来越复杂,我不知道如何处理它。这是一个留言板帖子,其中有一些必需的文本字段和可选的图像上传。我需要:

  • 从EXIF数据中找到图像的方向,并根据需要重新确定方向
  • 将原始映像的副本保存到服务器(完成)
  • 创建图像的缩略图并将其保存到服务器(完成)
  • 将记录保存到数据库,无论是否有上载的图像(完成)
  • 我关心我做事的顺序,想知道是否有更有效的方法。我知道我可以在路由内调用
    upload
    ,而不是将其传入,但我不想在将记录保存到数据库时重复自己的操作,因为无论是否有图像,我都需要保存它

    我有最后3个步骤的代码,但我愿意接受关于如何改进的建议。对于第一步,我很困惑如何获得原稿的方向并在需要时旋转它。这是我需要在客户端做的事情吗?如何将其应用到现有代码中

    代码如下:

    设置

    途中

    router.post('/new',upload.single('photo'),function(req,res){
        var photo = null;
        var allowedTypes = ['image/jpeg','image/gif','image/png'];
        if (req.file){
                photo = '/uploads/' + req.file.filename;
                // save thumbnail -- should this part go elsewhere?
                im.crop({
                  srcPath: './public/uploads/'+ req.file.filename,
                  dstPath: './public/uploads/thumbs/100x100/'+ req.file.filename,
                  width: 100,
                  height: 100
                }, function(err, stdout, stderr){
                  if (err) throw err;
                  console.log('100x100 thumbnail created');
                });
    
                // I can get orientation here, 
                // but the image has already been saved
                im.readMetadata('./public/uploads/'+ req.file.filename, function(err, metadata){
                  if (err) throw err;
                  console.log("exif orientation: " + metadata.exif.orientation);
                });
    
        }
    
        // Save it
        new Post({
            username: req.user.username,
            title: req.body.title,
            body: req.body.messagebody,
            photo: photo 
        }).save(function(err){
            if (err){ console.log(err); }
            res.redirect('/messageboard');
        });
    });
    
    谢谢你的帮助

    router.post('/new',upload.single('photo'),function(req,res){
        var photo = null;
        var allowedTypes = ['image/jpeg','image/gif','image/png'];
        if (req.file){
                photo = '/uploads/' + req.file.filename;
                // save thumbnail -- should this part go elsewhere?
                im.crop({
                  srcPath: './public/uploads/'+ req.file.filename,
                  dstPath: './public/uploads/thumbs/100x100/'+ req.file.filename,
                  width: 100,
                  height: 100
                }, function(err, stdout, stderr){
                  if (err) throw err;
                  console.log('100x100 thumbnail created');
                });
    
                // I can get orientation here, 
                // but the image has already been saved
                im.readMetadata('./public/uploads/'+ req.file.filename, function(err, metadata){
                  if (err) throw err;
                  console.log("exif orientation: " + metadata.exif.orientation);
                });
    
        }
    
        // Save it
        new Post({
            username: req.user.username,
            title: req.body.title,
            body: req.body.messagebody,
            photo: photo 
        }).save(function(err){
            if (err){ console.log(err); }
            res.redirect('/messageboard');
        });
    });