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

Overview

lqip-modern

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

NPM Build Status JavaScript Style Guide

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 resuls 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:

  • Hide the artifacts around the edges of the blurred images.
  • Provide an aesthetically pleasing feeling of zooming into the original image once it's loaded.

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

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

lqipModern

  • input (Buffer | string | Array<Buffer> | Array<string>) Either an array of image inputs or a single image input. Each image input may either be a Buffer containing raw image data, or a string containing the filesystem path to a supported image type.
  • opts Object? Optional configuration options.
    • opts.concurrency number Concurrency when processing an array of input images. (optional, default 4)
    • opts.outputFormat string Output format to use; either webp or jpeg (passing jpg is the same as passing jpeg). (optional, default 'webp')
    • opts.outputOptions Object? Output options passed to either sharp.webp or sharp.jpeg dependent on opts.outputFormat.
    • opts.resize (number | Array<any>)? Options to pass to sharp.resize. Defaults to resizing inputs to a max dimension of 16, with the other dimension being calculated to maintain aspect ratio. If you want more control, you can pass an array of args here which will be forwarded to sharp.resize.

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

Approach format Width Avg Encode Speed Avg Size
lqip-modern 🔥 webp 16px 0.011s 152 B
lqip-modern jpeg 16px 0.003s 274 B
lqip-modern webp 8px 0.014s 129 B
lqip-modern jpeg 8px 0.003s 244 B
lqip-modern webp 32px 0.013s 257 B
lqip-modern jpeg 32px 0.002s 347 B
lqip (original) jpeg 10px 0.395s 887 B
lqip-custom jpeg 32px 0.040s 545 B
sqip (default) svg original 1.468s 509 B

Check out the demo for full results.

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

Related

  • lqip - Original Low Quality Image Placeholders (LQIP) module.
  • sqip - Really solid SVG-based LQIP alternative.
    • See their comprehensive comparison of LQIP techniques.
    • The biggest disadvantage of this approach is that it's ~10-100x slower to compute these images.
  • blurhash - Really nice, compact placeholder images.
    • Requires non-native client-side decoding which makes it awkward and slow for browser usage.
    • Encoding speed is pretty slow (on par with sqip).
    • Under the hood, the webp format performs a similar set of transforms as the one used by blurhash.

License

MIT © Travis Fischer

Support my OSS work by following me on twitter twitter

You might also like...

This project is a web application that allows the user to select a location on a map, display the selected region, and capture an image of that region. The captured image is then applied as a texture to a 3D cuboid using BabylonJS.

Map-Babylon Client repo commit details https://github.com/hashmat-noorani/mapbox-babylon-client Server repo commit details https://github.com/hashmat

Mar 21, 2023

Tooltip using only CSS and very low build size.

Tooltip using only CSS and very low build size.

css-only-tooltip A very lightweight tooltip utitlity library, made using only CSS with dynamic light and dark themes. Insatallation Using npm $ npm in

Dec 11, 2022

This is a demo of updating a map to show air quality data for the user’s current location using Next.js Advanced Middleware, powered by Netlify Edge Functions.

Show Local Air Quality Based on the User's Location Use AQI data to show the air quality near the current user. This is built using Next.js Advanced M

Nov 4, 2022

A low-feature, dependency-free and performant test runner inspired by Rust and Deno

minitest A low-feature, dependency-free and performant test runner inspired by Rust and Deno Simplicity: Use the mt test runner with the test function

Nov 12, 2022

A Javascript library that discourages and prevents image theft/download by preventing client ability to retrieve the image.

ProtectImage.js ProtectImage.js is a Javascript library that helps prevent image theft by disabling traditional user interactions to download/copy ima

Aug 18, 2022

A fully-typed, low-level, and HyperScript-like Frontend Library 🚀

A fully-typed, low-level, and HyperScript-like Frontend Library 🚀

Apr 4, 2022

ToolJet an open-source low-code framework to build and deploy internal tools quickly without much effort from the engineering teams

ToolJet an open-source low-code framework to build and deploy internal tools quickly without much effort from the engineering teams

ToolJet is an open-source low-code framework to build and deploy internal tools quickly without much effort from the engineering teams. You can connect to your data sources, such as databases (like PostgreSQL, MongoDB, Elasticsearch, etc), API endpoints (ToolJet supports importing OpenAPI spec & OAuth2 authorization), and external services (like Stripe, Slack, Google Sheets, Airtable) and use our pre-built UI widgets to build internal tools.

Jan 3, 2023

Bitloops is Low-Code Workflow Orchestration platform that helps you build backend systems and APIs 10x faster.

Bitloops is Low-Code Workflow Orchestration platform that helps you build backend systems and APIs 10x faster.

Bitloops Bitloops is a scalable open source Firebase substitute that can support any database and workflow orchestration. We’re building Bitloops usin

Aug 9, 2022
Comments
  • chore: update sharp so it works in latest Apple Silicon

    chore: update sharp so it works in latest Apple Silicon

    Sharp does not work well on an M1 chip prior to version 0.28 described here:

    https://github.com/lovell/sharp/issues/2460#issuecomment-783401553

    I've tested this a deployed it on drew.tech, which is a fork of https://github.com/dbredvick/nextjs-notion-starter-kit and it's working great.

    opened by dbredvick 1
Owner
Travis Fischer
My heart is open source 💕 Formerly @amazon, @microsoft
Travis Fischer
Holder renders image placeholders in browser using SVG

Holder Holder renders image placeholders in browser using SVG. Used by thousands of open source projects (including Bootstrap v3) and many other sites

Ivan Malopinsky 5.8k Jan 2, 2023
Low cost, low effort P2P WebRTC serverless signalling using Cloudflare Workers

P2PCF P2PCF enables free (or cheap) serverless WebRTC signalling using a Cloudflare worker and a Cloudflare R2 bucket. The API is inspired by P2PT, bu

Greg Fodor 560 Jan 8, 2023
Small PNG to WEBP converter, terminal based and handy for a frontend developer :D

Image Utils A small util i used while making antstack.com What does it do and how to use? It basically converts .png files to .webp Read about Webp he

Pruthvi Shetty 6 Aug 24, 2022
A low-quality mod made by a rookie

Unstable-Industrial A low-quality mod made by a rookie There may be many grammatical mistakes in the descriptions because of my poor English ability.I

Niruvu Rūche 3 Dec 7, 2022
Formats message strings with number, date, plural, and select placeholders to create localized messages

Formats message strings with number, date, plural, and select placeholders to create localized messages. Small. Between 700 bytes and 1.3 kilobytes (m

Marcis Bergmanis 35 Oct 30, 2022
optimize image & upload file to cloud as image bed with tiny image automic.

Rush! 图片压缩 & 直传图床工具 这是一个兴趣使然的项目, 希望 Rush! 能让这个世界的网络资源浪费减少一点点 下载 Downloads 获取最新发行版 功能 Features 拖拽批量压缩图片, 支持格式 jpg/png/gif Drop to optimize, jpg/png/gif

{ Chao } 3 Nov 12, 2022
Full text search based on InvertedIndex and ordinary approach

The Node js project that has CRUD operation and has a FullTextSearch.

Ali Nowrouzi 5 Jul 15, 2022
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022
A novel approach for security and user experience of Graphical Password Authentication.

Graphical Password Authentication Alohomora Harry Potter themed (not really) Graphical Password Authentication Flowchart and Architecture Solution Dem

Akshat Shah 10 Dec 15, 2022