brianmcdo/image-palette

Extracts colors from an image and generates a color palette against a whitelist of colors.

v2.0.1 2015-10-25 01:38 UTC

This package is auto-updated.

Last update: 2024-10-26 04:31:06 UTC


README

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality

ImagePalette is used to extract a color palette from a given image. Aside from being a native PHP implementation, ImagePalette differs from many palette extractors as it works off a white list color palette. Below is the default palette:

The main advantage of working from a color palette is closer matching, as each pixel simply has to calculate the color-distance within the palette and chose the best match. This is useful for working with color taxonomies as the taxonomy should have a finite amount of colors.

See an example of this in action here: http://alpha.wallhaven.cc/wallpaper/21852

Requirements

PHP >= 5.4 php5-gd

Installation

Simply add the following to your composer.json file:

"require": {
    "brianmcdo/image-palette": "dev-master"
}

Usage

// initiate with image
$palette = new \BrianMcdo\ImagePalette\ImagePalette( 'https://www.google.co.uk/images/srpr/logo3w.png' );

// get the prominent colors
$colors = $palette->colors; // array of Color objects

// to string as json
echo $palette; // '["#ffffdd", ... ]'

// implements IteratorAggregate
foreach ($palette as $color) {
  // Color provides several getters/properties
  echo $color;             // '#ffffdd'
  echo $color->rgbString;  // 'rgb(255,255,221)'
  echo $color->rgbaString; // 'rgba(255,255,221,0.25)'
  echo $color->int;        // 0xffffdd
  echo $color->rgb;        // array(255,255,221)
  echo $color->rgba;       // array(255,255,221,0.25)
  // ...
}

And there we go!

Laravel 4

Find the providers key in app/config/app.php and register the ImagePaletteServiceProvider:

'providers' => array(
    // ...
    'BrianMcdo\ImagePalette\Laravel\ImagePaletteServiceProvider',
)

Then, find the aliases key in app/config/app.php and register the ImagePaletteFacade:

'aliases' => array(
    // ...
    'ImagePalette' => 'BrianMcdo\ImagePalette\Laravel\ImagePaletteFacade',
)

Example:

$fileOrUrl = 'https://www.google.com/images/srpr/logo11w.png';

ImagePalette::getColors($fileOrUrl);

Options

Precision

By default, ImagePalette will process every 10th pixel. This is for performance reasons, you can change this like below. The precision is a performance-to-time decision.

$palette = new \BrianMcdo\ImagePalette\ImagePalette( $src, 5 /* precision */ );

Color Count

To control the amount colors returned set the third parameter. You can also provide the getter with a custom length.

$palette = new \BrianMcdo\ImagePalette\ImagePalette( $src, 5, 3 /* number of colors to return */ );
$colors = $palette->getColors(7 /* number of colors to return */);

Contribution guidelines

See: https://github.com/brianmcdo/ImagePalette/blob/master/CONTRIBUTING.md