DEV Community

Cover image for Node JS Resize Image Upload Using Multer Sharp With Example
Robert Look
Robert Look

Posted on

Node JS Resize Image Upload Using Multer Sharp With Example

Resize image upload using multer, node js, and express js. In this tutorial, we learn node js resize an image using multer, sharp with node js, and express js. we learn javascript resizing images.

Image upload is the basic requirement of any project. So this example will guide you step by step on how to upload multiple images using multer Express js Framework. And you can understand the concept of multiple image uploads easily to use.

Node JS Resize Image Upload Using Multer Sharp With Example

Install Express and Multer Dependencies

npm install express multer --save
npm install sharp --save

Import Dependencies in Server.js File

const express = require('express');
const multer = require('multer');
const path = require('path');
const sharp = require('sharp');
Enter fullscreen mode Exit fullscreen mode

Create Server.js File

const express = require('express');
const multer = require('multer');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs');
const app = express();
const port = 3000

const storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, 'uploads/');
    },

    filename: function(req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
});
Enter fullscreen mode Exit fullscreen mode

.....

Original Source: https://www.phpcodingstuff.com/blog/node-js-resize-image-upload-using-multer-sharp-with-example.html

Top comments (0)