Is there any way to get dimensions of a picture before to upload it with Multer on Node.Js ?
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.
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");
});
});
})
And my form:
<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>
Is there any way to do it with the npm package ?
I finally place my pictures in a tmp folder before to get their dimensions.