kiritokatklian / laravel-color-palette
Laravel Wrapper for `ksubileau/color-thief-php`. Grabs the dominant color or a representative color palette from an image. Uses PHP and GD or Imagick.
Package info
github.com/kiritokatklian/laravel-color-palette
pkg:composer/kiritokatklian/laravel-color-palette
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/support: ^11.0|^12.0|^13.0
- ksubileau/color-thief-php: ^v2.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.0|^10.0|^11.0
- phpunit/phpunit: ^11.0|^12.0|^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel Wrapper for Color-Thief-PHP with additional changes. Grabs the dominant color or a representative color palette from an image. Uses PHP and GD or Imagick.
This Laravel package is extremely useful to grab dominant color or a representative color palette from images. See this image for the example.
Contents
Installation
You can install the package via Composer:
$ composer require kiritokatklian/laravel-color-palette
You must install the service provider (For Laravel < 5.5):
// config/app.php 'providers' => [ ... Kiritokatklian\LaravelColorPalette\ColorPaletteServiceProvider::class, ],
Register facade:
// config/app.php 'aliases' => [ ... 'ColorPalette' => Kiritokatklian\LaravelColorPalette\ColorPaletteFacade::class, ],
Available Methods
-
getColor() - Use this method to get most dominant single color form image
Example:
// get most dominant color from image $color = ColorPalette::getColor( 'https://rawcdn.githack.com/kiritokatklian/laravel-color-palette/master/tests/images/strawberry.jpeg' ); // Color provides several getters/properties echo $color; // '#dc5550' echo $color->rgbString; // 'rgb(220,85,80)' echo $color->rgbaString; // 'rgba(220,85,80,1)' echo $color->int; // 14439760 print_r($color->rgb); // array(220, 85, 80) print_r($color->rgba); // array(220, 85, 80, 1)
Options
$color = ColorPalette::getColor($sourceImage, $quality = 10, $area = null );
By default,
getColorwill have quality -> 10 and specific area -> null.Qualitycan be int. 1 is the highest quality. There is a trade-off between quality and speed. The bigger the number, the faster the palette generation but the greater the likelihood that colors will be missed.Areacan be array|null $area[x,y,w,h]. It allows you to specify a rectangular area in the image in order to get colors only for this area. It needs to be an associative array with the following keys:- $area['x']: The x-coordinate of the top left corner of the area. Default to 0.
- $area['y']: The y-coordinate of the top left corner of the area. Default to 0.
- $area['w']: The width of the area. Default to image width minus x-coordinate.
- $area['h']: The height of the area. Default to image height minus y-coordinate.
-
getPalette() - Use this method to find representative color palette form image.
Example:
// get colors from image $colors = ColorPalette::getPalette( 'https://github.com/kiritokatklian/laravel-color-palette/blob/master/tests/images/strawberry.jpeg' ); foreach($colors as $color) { // } // Colors will be array of Color Objects
Options
$color = ColorPalette::getPalette($sourceImage, $colorCount = 10, $quality = 10, $area = null)
colorCountcan be 2 to 256. It is the number of colors you want to retrieve for the image.Quality&Areais same as above.
Color Spaces
Colors keep the values of the image's pixels. Every color has a colorSpace that tells which color space those values are in:
use Kiritokatklian\LaravelColorPalette\ColorSpace; $color = ColorPalette::getColor($sourceImage); $color->colorSpace; // ColorSpace::SRGB, DisplayP3, AdobeRGB, ProPhotoRGB or Rec2020
With Imagick installed, the color space comes from the image's embedded color profile. Images without a profile, or with a profile of any other color space, return ColorSpace::SRGB. GD reads no color profiles, so its colors are always ColorSpace::SRGB.
Test
You can run the tests with:
$ composer run test
Credits
A big thank you to Nikunj Kanetiya for creating the first version of this package.
Image Source: https://www.pexels.com, google image
License
The MIT License (MIT). Please see License File for more information.