Home

Awesome

lqip-modern

Modern approach to Low Quality Image Placeholders (LQIP) using webp and sharp. (demo)

NPM Build Status Prettier Code Formatting

This approach is extremely fast and produces much smaller outputs than alternatives.

Examples

Check out the demo for more examples and details.

How It Works

This package uses a very similar LQIP approach to the one used by Medium.

We use sharp to resize input images to a max dimension of 16px and output webp (default) or jpeg images with an encoding quality set to 20. The max dimension is a single, simple variable to tradeoff between encoded image size and visual fidelity.

This results in very efficient placeholder images that have noticeable artifacts due to the low quality encoding. These artifacts are then hidden in the browser using a simple blur filter.

.placeholder {
  filter: blur(20px);
  transform: scale(1.1);
}

Note that Medium uses this scale transform on its placeholder images for two reasons:

An alternative to using this blur + transform technique is to use a CSS backdrop-filter. This technique has less cross-browser support, but it produces clean blurred preview images without the need to transform the placeholder.

.placeholder::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  backdrop-filter: blur(20px);
  pointer-events: none;
}

Install

npm install --save lqip-modern
## or
yarn add lqip-modern
## or
pnpm add lqip-modern

Usage

const lqip = require('lqip-modern')
const result = await lqip('fixtures/brooklyn.jpg')

which outputs

{
  content: <Buffer>,
  metadata: {
    originalWidth: 1400,
    originalHeight: 350,
    width: 16,
    height: 4,
    type: 'webp',
    dataURIBase64: 'data:image/webp;base64,UklGRkIAAABXRUJQVlA4IDYAAADQAQCdASoQAAQABUB8JYgCdADjazMu8AD+flCYsVr2GH6CLYVog1jhRLpBUIu8UmqhGnoAAAA='
  }
}

If you pass an array of inputs, the result will be an array of outputs.

The format of the output is as close to sqip as possible for easy comparison.

API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

lqipModern

Compatibility

Webp is supported by 98% of browsers and produces significantly smaller results.

If you need 100% browser support, then I recommend that you use the jpeg output format or sqip.

In the future, I'd love to experiment with outputting jpeg at full quality and then compressing the results with mozjpeg at 20% quality.

Comparison

ApproachformatWidthAvg Encode SpeedAvg Size
lqip-modern 🔥webp16px0.011s152 B
lqip-modernjpeg16px0.003s274 B
lqip-modernwebp8px0.014s129 B
lqip-modernjpeg8px0.003s244 B
lqip-modernwebp32px0.013s257 B
lqip-modernjpeg32px0.002s347 B
lqip (original)jpeg10px0.395s887 B
lqip-customjpeg32px0.040s545 B
sqip (default)svgoriginal1.468s509 B

Check out the demo for full results.

Generated with a fork of sqip's excellent comparison benchmark.

Related

License

MIT © Travis Fischer

Support my OSS work by <a href="https://twitter.com/transitive_bs">following me on twitter <img src="https://storage.googleapis.com/saasify-assets/twitter-logo.svg" alt="twitter" height="24px" align="center"></a>