pnpm add @oversightstudio/blur-data-urls sharp
This plugin automatically assigns URLs for blur data to media collections in Payload. It will automatically add the blurDataUrl field to the collections you provide and automatically generate and assign a blurDataUrl to the field whenever a media item is uploaded.
import { buildConfig } from 'payload'
import { blurDataUrlsPlugin } from '@oversightstudio/blur-data-urls'
import { Media } from './collections/Media'
export default buildConfig({
plugins: [
blurDataUrlsPlugin({
enabled: true,
collections: [Media],
// Blur data URLs Settings (Optional)
blurOptions: {
blur: 18,
width: 32,
height: "auto",
}
}),
],
})| Option | Type | Default | Description |
|---|---|---|---|
enabled |
boolean |
true |
Whether the plugin is enabled. |
collections |
PluginCollectionConfig[] |
Required | A list of collections where blurDataUrl should be implemented. Should typically be the main media collection. |
blurOptions |
object |
Optional | Additional settings for generating blurDataUrls. |
| Option | Type | Default | Description |
|---|---|---|---|
width |
number |
32 |
Width of the blurDataUrl. |
height |
number | 'auto' |
'auto' |
Height of the blurDataUrl. If 'auto', it maintains the image’s aspect ratio. |
blur |
number |
18 |
The amount of blur applied to the generated blurDataUrl. |
import Image from 'next/image'
<Image
// Your other image settings
placeholder="blur"
blurDataURL={image.blurDataUrl ?? undefined}
/>If you already have images in your media collection, you might want to generate blurDataUrls for them.
To do so:
- Place this script on your
./src/scripts/ directory. - Make sure you have
tsxinstalled either globally or on your project. You can uninstall it after running the script. - Modify the plugin configuration on the script. You'll be able to set the blur options + which collections to migrate.
- Run the script:
tsx ./src/scripts/blurDataUrlsMigrationScript.ts - Let it cook.
- Shoutout to Paul for being a real one.