zapal-tech

payload blurhash plugin

by zapal-tech
Media & images
v1
v2
^1.7.2 || ^2.0.0
MIT

Overview

Summary generated from the README

Adds a readonly blurhash field to upload-enabled collections you choose and computes a BlurHash string for each uploaded image. It is registered through the plugins array in the Payload config. Encoding can be tuned with resize width and height and X and Y component counts, and the plugin can be toggled on or off. Supports Payload 1.7.2 and 2.x; imageSizes are not yet supported.

Features

  • Generates a readonly blurhash field for each uploaded image
  • Applies only to the upload collections listed by slug
  • Configurable resize width and height before encoding
  • Configurable X and Y component counts for the encoder
  • Enable or disable the plugin through an enabled option

Use cases

  • Show lightweight blurred placeholders while images load on a Next.js site
  • Improve perceived page speed for image-heavy blogs or team photo galleries

From the author

Dead simple BlurHash auto generation plugin for Payload CMS

Installation

npm i @zapal/payload-blurhash-plugin

8

Stars

0

Forks

1

Open Issues

2 years ago

Last Updated

6

Weekly Downloads

v1.0.7

npm Version

43.0 KB

Unpacked Size

1

Dependencies

GitHub stars

Trends appear after 7 days of data. Daily snapshots have not started yet.

npm weekly downloads

Trends appear after 7 days of data. Daily snapshots have not started yet.

Each point is npm's rolling 7-day download count.

Health Score

33/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/706679910-root.svg)](https://payloaddirectory.dev/plugins/706679910-root)
HTML
<a href="https://payloaddirectory.dev/plugins/706679910-root"><img src="https://payloaddirectory.dev/badge/706679910-root.svg" alt="Payload Directory"></a>
Updated Dec 15, 2023Created Oct 18, 2023Published Oct 22, 2023

README

Payload CMS BlurHash Plugin by Zapal

Turbocharge your website performance by creating ultralight image placeholders, resulting in a smoother user experience.

📋 Requirements

  • Payload version 1.7.2 or higher is required (version 2.0.0 or higher is recommended)

🚀 Getting started

Instal the package with yarn add @zapal/payload-blurhash-plugin or npm install @zapal/payload-blurhash-plugin

Now setup the plugin within your payload.config.ts file as follows:

import { buildConfig } from 'payload/config';

import { blurHash } from '@zapal/payload-blurhash-plugin';
import path from 'path';

/* ... */

export default buildConfig({
  plugins: [
    blurHash({
      collections: ['team-photos-collection', 'blog-images-collection'],
    }),
  ],
  /* ... */
});

The plugin will generate a readonly blurhash field for each image uploaded to the specified collections. Generation for imageSizes are not supported yet (but it's in progress).

🎛️ Conditionally enabling/disabling

The proper way to conditionally enable/disable this plugin is to use the enabled property.

blurHash({
  enabled: process.env.ENABLE_BLURHASH_PLUGIN === 'true',
  collections: ['team-photos-collection', 'blog-images-collection'],
}),

⚙️ Plugin options

This plugin is configurable to work across upload-enabled Payload collections. A * denotes that the property is required.

OptionTypeDescription
collections *string[]Array of upload-enabled collections' slugs you want to enable the plugin for.
enabledbooleanConditionally enable/disable plugin. Default: true.
widthnumberWidth to resize the image before computing the blurhash. Default: 32.
heightnumberHeight to resize the image before computing the blurhash. Default: 32.
componentXnumberX component count to pass to the blurhash encoder. Default: 3.
componentYnumberY component count to pass to the blurhash encoder. Default: 3.

See more information about the width, height, componentX, componentY options and BlurHash encoder here.

💻 Local development

You can find the source code in the src directory. For development purposes, you can use the dev directory with all the necessary files to test the plugin in a Payload project. Also we are strongly recommending to use your IDE debugger instead of console.log statements.

You need to follow these steps to start developing and testing the plugin locally:

  1. Go to dev directory (cd dev)
  2. Create .env file (cp .env.example .env)
  3. Setup environment variables in .env file
  4. Install dependencies (npm i)
  5. Run the project (npm run dev)

And you are ready to go!

🤝 Contributing

We are open to, and grateful for, any contributions made by the community.

We are planning to add more features to this plugin in the future. If you have any ideas, create an issue to discuss it.

Next steps (v1):

  • Optional blurhash generation for imageSizes (in progress)
  • Custom field name support
  • Unit tests
  • Code refactoring

Next steps (v2):

  • Custom options for each collection

🛡️ License

This project is licensed under the MIT License

⭐ Like what we're doing? Give us a star

Similar plugins