So I searched for a long time to try to find a way to compress user images before they are uploaded.
Imagemin seems to be the most widely used package atm for Node.js in that regard.
I looked at the documentation. And I honestly don't understand at all how to use the package.
I wasn't able to find how to use it anywhere.
Here is my current code for uploading files:
router.post("/", function(req, res, next){
upload(req,res, function (err) {
const { token } = req.cookies;
if (!token) {
global.page_name = "login";
res.render("users/login");
}
else {
admin.auth().verifyIdToken(token)
.then(decodedToken => {
const uid = decodedToken.sub;
if (err) {
req.flash("error_msg", "Upload failed. Please try again. Info: Maximum file size is 500KB in case your file is too large.");
res.redirect("upload");
return
}
var title = req.body.title;
var section = req.body.section;
req.checkBody('title', 'Title is required').notEmpty();
req.checkBody('title', 'Title is too long, max: 30 charachters !').len(2,30);
req.checkBody('game', 'Game Name is too long, max: 30 charachters !').len(0,30);
req.checkBody('section', 'Section is required').notEmpty();
var errors = req.validationErrors();
if(errors){
res.render('upload', {
errors: errors
});
}
else {
if (req.file){
verifyRecaptcha(req.body["g-recaptcha-response"], function(success) {
if (success) {
var now = global.serverTime;
var userRef = admin.database().ref("users/"+uid);
userRef.once("value", function(user){
var userData = user.val();
var image = req.file.filename;
var section = req.body.section.toLowerCase();
bucket.upload("./public/images/uploads/"+req.file.filename, function(err, file) {
if (!err) {
fs.unlink("./public/images/uploads/"+req.file.filename, (err) => {
if (err) {
} else {
}
});
//ETC ...
How can I user Imagemin to compress the image before uploading it ?
Or maybe you know another package/library/method that is better ?
via TheProgrammer
No comments:
Post a Comment