Coder Social home page Coder Social logo

multer-resizer's Introduction

Multer Resizer

Multer Resizer is an addon for the commonly used express library multer. It extends the file upload feature of multer and transforms image-resizing to a piece of cake.

Traditional image upload using multer:

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

With Multer Resizer, you get the original file and also additional resized versions of it:

app.post('/profile', resizer.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.file.coverPath, req.file.resizedPath etc...
})

Installation

I am not going to publish my projects to npm anymore. You can install multer-resizer through this github repository using the command below:

$ npm install mertdogar/multer-resizer

Configurations

Configuring multer resizer is easy, just declare tasks in an array. Any number of tasks are accepted.

const MulterResizer = require('multer-resizer');
const resizer = new MulterResizer({
    multer: uploader,
    tasks: [
        {...},
        {...}
    ]
});

Multer Instance

Multer resizer is not a multipart file handler, it just extends an existing multer instance. Therefore, you must provide a properly configured multer instance to make it work.

    multer: uploader

Tasks

Resize

Resizes an image. The image is resized to the given dimension and the aspect ratio of the resized version may not be the same with the original image.

Usage
{
    resize: {
        width: 1920,
        height: 1080,
        interpolation: 'linear',
        format: 'png'
    }
}
Option Description
interpolation 'nearest-neighbor', 'moving-average', 'linear', 'grid', 'cubic', 'lanczos' default: 'linear'
format 'png', 'jpg', 'gif' default: 'png'
suffix default: 'resized'
width Width in pixels
height Height in pixels
Cover

Cover a canvas with the image. The image will be resized to the smallest possible size such that both its dimensions are bigger than the canvas's dimensions. Margins of the image exceeding the canvas will be discarded.

Usage
{
    cover: {
        width: 800,
        height: 600,
        interpolation: 'linear',
        format: 'png'
    }
}
Option Description
interpolation 'nearest-neighbor', 'moving-average', 'linear', 'grid', 'cubic', 'lanczos' default: 'linear'
format 'png', 'jpg', 'gif' default: 'png'
suffix default: 'cover'
width Width in pixels
height Height in pixels

Example

const multer = require('multer');
const MulterResizer = require('multer-resizer');
const resizer = new MulterResizer({
    multer: multer({storage: multer.diskStorage({destination: './'})}),
    tasks: [
        {
            resize: {
                width: 1920,
                height: 1080,
                suffix: 'resized-big'
            }
        },
        {
            resize: {
                width: 100,
                height: 100,
                suffix: 'resized-small'
            }
        },
        {
            cover: {
                width: 160,
                height: 112,
                suffix: 'thumbnail'
            }
        }
    ]
});

router.post('/', auth.ensureAuthentication, resizer.single('file'), function(req, res, next) {
    // All multer-resizer tasks were completed
});

Dependencies

Nodejs 4+

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.