brianmcdo / image-palette
Extracts colors from an image and generates a color palette against a whitelist of colors.
Installs: 190 299
Dependents: 2
Suggesters: 0
Security: 0
Stars: 180
Watchers: 7
Forks: 87
Open Issues: 7
Requires
- php: >=5.4.0
- ext-gd: *
Replaces
This package is auto-updated.
Last update: 2024-10-26 04:31:06 UTC
README
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