A Strapi plugin that extends image uploads to generate and attach a color palette to the schema when uploaded.
This plugin generates a color palette and dominant color for images uploaded to Strapi. It uses GraphicsMagick to extract the colors from the image after it's uploaded, and stores them in the database schema. When queried, it looks like this:
1...
2colors: {
3 dominant: "#534f70",
4 palette: [ "#042d65", "#43374b", "#f96597", "#77c6ff", "#e1e203" ]
5}
6...
This can be useful for adding color accents, or for using the colors as a placeholder while the image is loading.
Photo by Thomas McPherson on Unsplash
This plugin supports most major image formats.
It also now supports SVG files but due to some limitations, it will attach the full palette regardless of what is set as the paletteSize
. The dominant color will be set to the first color in this array, so it won't always be accurate.
This plugin is for Strapi v4.
You'll also need to have GraphicsMagick installed on your system. You can install it with Homebrew on macOS with the following command:
brew install graphicsmagick
On Heroku, you'll need to add the GraphicsMagick buildpack to your app.
Install the plugin via Yarn:
yarn add @studio123/strapi-plugin-image-color-palette
Append the following to your Strapi plugin config file (config/plugins.js
) and adjust as needed:
1"image-color-palette": {
2 enabled: true,
3 config: {
4 format: "rgb",
5 paletteSize: 4,
6 }
7}
Restart your Strapi server.
The plugin offers the following configuration options:
Option | Description |
---|---|
format | The format to return the colors in. Available options are hex , rgb , hsl , and raw .Default: raw |
paletteSize | The number of colors to generate in the color palette. Accepts an integer between 1-8. Default: 4 |
The plugin can return the colors in the following formats:
1 raw: { r: 255, g: 255, b: 255 },
2 hex: '#ffffff',
3 rgb: 'rgb(255, 255, 255)',
4 hsl: 'hsl(0, 0%, 100%)',
To add color palette data to existing images, you'll need to add the following script to the ./database/migrations
folder in your Strapi project. You can name it anything you want, but it's recommended to use a timestamp as the prefix. It will run automatically when you start your Strapi server, so be sure to backup your database before running it.
Important: Make sure you start Strapi after installation so that the database schema is updated with the new colors
column. Then, you can add the migration script and start Strapi again.
1'use strict';
2
3const FILES_TABLE = 'files'; // Change this if you're using a custom table name
4const BATCH_SIZE = 1000; // Batch size for querying the database
5
6async function up(trx) {
7 let lastId = 0;
8
9 while (true) {
10 const files = await trx
11 .select(['id', 'url', 'mime'])
12 .from(FILES_TABLE)
13 .whereNot('url', null)
14 .andWhereLike('mime', 'image/%')
15 .andWhere('colors', null)
16 .andWhere('id', '>', lastId)
17 .orderBy('id', 'asc')
18 .limit(BATCH_SIZE);
19
20 for (const file of files) {
21 const colorPalette = await strapi
22 .plugin('image-color-palette')
23 .service('image-color-palette')
24 .generate(file.url, file.mime);
25
26 if (colorPalette) {
27 await trx
28 .update('colors', colorPalette)
29 .from(FILES_TABLE)
30 .where('id', file.id)
31 .catch(err => {
32 strapi.log.warn(
33 `Error adding or updating color data for file ${file.id}: ${err}`,
34 );
35 })
36 .then(() => {
37 strapi.log.info(`Added color data to file ${file.id} successfully.`);
38 });
39 } else {
40 strapi.log.warn(`Could not generate color data for file ${file.id}.`);
41 }
42 }
43
44 if (files.length < BATCH_SIZE) {
45 break;
46 }
47
48 lastId = files[files.length - 1].id;
49 }
50}
51
52async function down() {}
53
54module.exports = { up, down };
To contribute to this plugin, please open an issue or submit a pull request.
npm install @studio123/strapi-plugin-image-color-palette
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.