npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@studio123/strapi-plugin-image-color-palette

v1.1.3

Published

A Strapi plugin that extends image uploads to generate and attach a color palette to the schema when uploaded.

Downloads

3

Readme

🎨 Strapi Plugin: Image Color Palette

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:

...
colors: {
  dominant: "#534f70",
  palette: [ "#042d65", "#43374b", "#f96597", "#77c6ff", "#e1e203" ]
}
...

This can be useful for adding color accents, or for using the colors as a placeholder while the image is loading.

Supported Formats

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.

Requirements

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

Heroku

On Heroku, you'll need to add the GraphicsMagick buildpack to your app.

Installation

  1. Install the plugin via Yarn:

    yarn add @studio123/strapi-plugin-image-color-palette
  2. Append the following to your Strapi plugin config file (config/plugins.js) and adjust as needed:

    "image-color-palette": {
        enabled: true,
        config: {
            format: "rgb",
            paletteSize: 4,
        }
    }
  3. Restart your Strapi server.

Configuration

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 |

Format Examples

The plugin can return the colors in the following formats:

    raw: { r: 255, g: 255, b: 255 },
    hex: '#ffffff',
    rgb: 'rgb(255, 255, 255)',
    hsl: 'hsl(0, 0%, 100%)',

Migration

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.

'use strict';

const FILES_TABLE = 'files'; // Change this if you're using a custom table name
const BATCH_SIZE = 1000; // Batch size for querying the database

async function up(trx) {
    let lastId = 0;

    while (true) {
        const files = await trx
            .select(['id', 'url', 'mime'])
            .from(FILES_TABLE)
            .whereNot('url', null)
            .andWhereLike('mime', 'image/%')
            .andWhere('colors', null)
            .andWhere('id', '>', lastId)
            .orderBy('id', 'asc')
            .limit(BATCH_SIZE);

        for (const file of files) {
            const colorPalette = await strapi
                .plugin('image-color-palette')
                .service('image-color-palette')
                .generate(file.url, file.mime);

            if (colorPalette) {
                await trx
                    .update('colors', colorPalette)
                    .from(FILES_TABLE)
                    .where('id', file.id)
                    .catch(err => {
                        strapi.log.warn(
                            `Error adding or updating color data for file ${file.id}: ${err}`,
                        );
                    })
                    .then(() => {
                        strapi.log.info(`Added color data to file ${file.id} successfully.`);
                    });
            } else {
                strapi.log.warn(`Could not generate color data for file ${file.id}.`);
            }
        }

        if (files.length < BATCH_SIZE) {
            break;
        }

        lastId = files[files.length - 1].id;
    }
}

async function down() {}

module.exports = { up, down };

Contributing

To contribute to this plugin, please open an issue or submit a pull request.