本文介绍了使用Multer获取图片尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在用Multer在Node.Js上载图片之前,有什么方法可以获取图片的尺寸吗?

Is there any way to get dimensions of a picture before to upload it with Multer on Node.Js ?

我有一个应用程序,可以从表单获取图片并将其上传到库中.我想做的是,如果该图片的宽度和高度小于300px,则停止上传图片.

I have an application which get pictures from a form and upload them in a repertory. What I'd like to do is to stop the picture upload if this one's width and height are under 300px.

这是Node.Js服务器应用程序

Here's the Node.Js server app

app.post("/upload", function (req, res, fields) {

    const storage = multer.diskStorage({
      destination: "public/data/",
      filename: function (req, file, cb) {
        crypto.randomBytes(30, (err, buf) => {
          cb(null, buf.toString("hex") + path.extname(file.originalname))
        })
      }
    });

    const upload = multer({
      storage: storage
    }).fields([{
      name: "pp"
    }, {
      name: "banner"
    }]);

    upload(req, res, (err) => {
      if (err) throw err;
      if (req.files.pp) {
        var PPsrc = req.files.pp[0].path.slice(7);
        var hasPP = 1;
      } else {
        var PPsrc = null;
        var hasPP = 0;
      }
      if (req.files.banner) {
        var Bannersrc = req.files.banner[0].path.slice(7);
        var hasBanner = 1;
      } else {
        var Bannersrc = null;
        var hasBanner = 0;
      }

      con.query("SQL Request", [hasPP, PPsrc, hasBanner, Bannersrc, sess.email], function (err) {
        if (err) throw err;
        console.log("Profile Picture and banner updated for user: " + sess.email);
        res.redirect("/profile");
      });
    });
  })

我的表单:

<form action="/upload" enctype="multipart/form-data" method="post">
          <label for="pp_uploader">Add a profile picture</label>
          <input type="file" id="pp_uploader" name="pp" accept="image/jpeg, image/jpg"/><br>
          <label for="banner_uploader">Add a Banner</label>
          <input type="file" id="banner_uploader" name="banner" /><br>
          <input class="sbutton" type="submit" value="Envoyer" />
        </form>

使用npm软件包有什么办法吗?

Is there any way to do it with the npm package ?

推荐答案

我终于将图片放置在tmp文件夹中,然后获取它们的尺寸.

I finally place my pictures in a tmp folder before to get their dimensions.

这篇关于使用Multer获取图片尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:15