@naumovs/image-middleware-sharp

1.0.1 • Public • Published

@naumovs/image-middleware-sharp

Express/Connect middleware to transform images on-demand.

Usage

app.use(resizer({
  // Root path to the folder with original images
  root: 'public',
  // Root path to the folder with cached images
  cache: 'public',

  // list of transforms that are available
  transforms: {
    test(stream, sharp) {
      return stream
        .resize(200)
        .normalise()
        .crop();
    }
  }

  // prefix for your transforms in the url
  prefix: 't_',
}));

Given configuration above and the file available in public/uploads/file.png you will be able to access the transformed file with one of following requests

GET /uploads/t_test/file.png
GET /uploads/t_test/file.png.webp
HEAD /uploads/t_test/file.png
HEAD /uploads/t_test/file.png.webp

After first access a new file will be generated in cache folder at requested path. Next time it will serve cached file. If you want to omit cache add ?nocache to the url

Package Sidebar

Install

npm i @naumovs/image-middleware-sharp

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • naumovs