Code Monkey home page Code Monkey logo

image-process-loader's Introduction

image-process-loader

npm install --save-dev image-process-loader sharp
# or
yarn add -D image-process-loader sharp

Image process loader for webpack, powered by sharp

Usage

This loader was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.

You have full access to sharp API, take a look at their documentation starting from here, and know what methods you can call


Take an example from sharp resize method. Let's say I want to restrict all image's width to 200px

In sharp

sharp(inputBuffer)
  .resize(200)
  .toBuffer()

In image-process-loader

/* webpack.config.js */
module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|tiff|webp)$/,
        loader: 'image-process-loader',
        options: {
          resize: 200
        }
      }
    ]
  }
}

It is okay to pass multiple arguments, just wrap your option with array.

For example, if I want to resize all images to width=200px and height=300px

/* webpack.config.js */
module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|tiff|webp)$/,
        loader: 'image-process-loader',
        options: {
          resize: [200, 300]
        }
      }
    ]
  }
}

And of course you can pass sharp's Enums

/* webpack.config.js */
const sharp = require('sharp')

module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|tiff|webp)$/,
        loader: 'image-process-loader',
        options: {
          resize: [200, 300, {
            kernel: sharp.kernel.lanczos2,
            interpolator: sharp.interpolator.nohalo
          }]
        }
      }
    ]
  }
}

One important thing to keep in mind, the order of options is exactly the same as the order of processing image

In image-process-loader

/* webpack.config.js */
const sharp = require('sharp')

module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|tiff|webp)$/,
        loader: 'image-process-loader',
        options: {
          crop: sharp.strategy.entropy,
          resize: 200
        }
      }
    ]
  }
}

In sharp

sharp(inputBuffer)
  .crop(sharp.strategy.entropy)
  .resize(200)
  .toBuffer()

Examples

Convert all images to progressive jpeg

/* webpack.config.js */
module.exports = {
  module: {
    rules: [
      {
        test: /\.(gif|jpe?g|png|svg|tiff|webp)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'img/[name].jpg'
            }
          },
          {
            loader: 'image-process-loader',
            options: {
              jpeg: {
                progressive: true
              }
            }
          }
        ]
      }
    ]
  }
}

Convert all icons to greyscale when in development mode

/* webpack.config.js */

const rules = [
  {
    test: /\.png$/,
    loader: 'file-loader',
    options: {
      name: 'img/[name].[ext]'
    }
  }
]

if (process.env.NODE_ENV === 'development') {
  rules.push(
    {
      test: /\/icon.+\.png$/, // assume all icons have `icon` prefix
      loader: 'image-process-loader',
      options: {
        greyscale: true
      }
    }
  )
}

module.exports = {
  module: {
    rules: rules
  }
}

Use preset(s) and inline query params

/* webpack.config.js */
module.exports = {
  module: {
    rules: [
      {
        test: /\.(gif|jpe?g|png|svg|tiff|webp)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'img/[name].jpg'
            }
          },
          {
            loader: 'image-process-loader',
            options: {
              jpeg: {
                progressive: true
              },
              presets: {
                blur: {
                  blur: true,
                  jpeg: {
                    quality: 55,
                  },
                },
                good-quality: {
                  jpeg: {
                    quality: 80,
                  },
                }
              }
            }
          }
        ]
      }
    ]
  }
}
require('path/to/image.jpg?preset=blur'); // blur, quality: 55
require('path/to/image.jpg?presets[]=blur&presets[]=good-quality'); // blur, quality: 80; presets order matter
require('path/to/image.jpg?presets[]=good-quality&presets[]=blur'); // blur, quality: 55; presets order matter
require('path/to/image.jpg?{preset:blur,{jpeg:{quality:40}}}'); // blur, quality: 40

image-process-loader's People

Contributors

foray1010 avatar truongsinh avatar yannski avatar

Watchers

 avatar James Cloos avatar

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.