ksubileau/color-thief-php
Extract dominant colors and palettes from images in PHP. Color Thief PHP ports the MMCQ algorithm and works with GD, Imagick, or Gmagick. Supports JPEG, PNG, GIF, and WebP, and accepts paths, URLs, resources, objects, or binary data.
New features
readonly classes, enums, named arguments, ... (see #62, thank @joeworkman).getSwatches() method that classifies a palette into semantic swatch roles: vibrant, muted, darkVibrant, darkMuted, lightVibrant, lightMuted.whiteThreshold, alphaThreshold, and minSaturation options.colorSpace option. OKLCH produces more perceptually uniform palettes and is now the default.ImageRegion class to restrict color extraction to a rectangular region of the image, replacing the previous $area array parameter.toRgb(), toHsl(), toHsv(), toOklch(), toCmyk().toCss()), hex string (toHex()), packed integer (toInt()), component array (toArray()).luminance(), isDark(), isLight(), textColor().Bug fixes
getimagesize return value in GdAdapter::loadFromPath (see #62, thank @joeworkman).array_shift in GmagickAdapter::getPixelColor (see #62, thank @joeworkman).Breaking changes
ColorThief is now an instantiable readonly class. Static method calls (ColorThief::getColor(), ColorThief::getPalette()) are no longer supported; create an instance instead.quality and preferredAdapter are now constructor parameters. The $quality and $adapter positional arguments previously accepted by getColor() and getPalette() have been removed.$outputFormat parameter has been removed. getColor() now always returns an RgbColor object (or null) and getPalette() always returns a ColorPalette<RgbColor>. Use the conversion methods on the returned objects to get the format you need.$area array parameter has been replaced by a typed ImageRegion object passed as the $region named argument.getRed(), getGreen(), and getBlue() on the color object have been replaced by red(), green(), and blue() methods.getColor() or getPalette() now throws a NotReadableException.
URL fetching was removed because it can introduce Server-Side Request Forgery (SSRF) vulnerabilities if the URL is not validated before being passed to the library; responsibility for fetching remote images now lies with the caller.
Fetch the image yourself and pass the binary content instead.
// Before
$color = ColorThief::getColor('https://example.com/image.jpg');
// After
use ColorThief\ColorThief;
$thief = new ColorThief();
$data = file_get_contents('https://example.com/image.jpg'); // Ensure you implement appropriate security controls when fetching the image
$color = $thief->getColor($data);
colorSpace: ColorSpace::Rgb to restore the previous behavior.New features
Bug fix
New features
ColorThief\Color class).Bug fix
getPalette() does not always return the requested number of colors (see #5).Breaking changes
ColorThief\Exception\Exception.
Migrating from 1.x may require tweaking exception handling in calling code to avoid unhandled exceptions or preserve error handling logic. See 1bf90f40 for details.Noticeable changes
How can I help you explore Laravel packages today?