How can i resize uploaded image to some specific height and width using express fileupload

1.2k views Asked by At

hi I'm using express file upload for uploading images in my NodeJS project but I need to resize the uploading image to 1000*1000 size and save to folder how can i do that

router.post('/add-product', (req, res) => {
// console.log(req.body)
let product=req.body
product.price=parseInt(product.price)
console.log(req.files.image);

let imgCount = req.body.len
producthelper.addProduct(req.body,(id)=>{
    let image=req.files.image

    if(imgCount == 1)
    {
        image.mv('./public/images/product-images/'+id+'-'+0+'.jpg',(err,done)=>{
            if(err)
            {
                console.log(err)
            }
        })
    }
    else{
        for(let i=0;i<imgCount;i++)
        {
            image[i].mv('./public/images/product-images/'+id+'-'+i+'.jpg',(err,done)=>{
                if(err)
                {
                    console.log(err)
                }
            }) 
        }
    }
    req.flash('success_msg',"Successfully Added Your Product for Sell...")
    res.redirect('/sellproduct')
})

})

this is my code to save the image in the folder how to replace the same?

1

There are 1 answers

1
Ayzrian On BEST ANSWER

You can use sharp npm package for that. For example.

sharp(inputBuffer)
   .resize(1000, 1000)
   .toFile('outputimage.jpeg', (err, info) => { ... });

https://www.npmjs.com/package/sharp