1. Go to this page and download the library: Download ksubileau/color-thief-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$swatches = $thief->getSwatches($sourceImage);
echo $swatches->vibrant?->toHex('#'); // e.g. "#e03c7d"
echo $swatches->darkVibrant?->toHex('#'); // e.g. "#7b1c2e"
echo $swatches->muted?->toHex('#'); // e.g. "#b07a92"
$color = $thief->getColor('/path/to/image.jpg');
// CSS representations
$color->toCss(); // "rgb(98, 42, 131)"
$color->toOklch()->toCss(); // "oklch(0.3721 0.1584 307.45)"
$color->toHsl()->toCss(); // "hsl(277, 52%, 34%)"
// Hexadecimal
$color->toHex(); // "622a83"
$color->toHex('#'); // "#622a83"
// Component arrays
$color->toArray(); // [98, 42, 131]
$color->toHsl()->toArray(); // [276.92, 0.515, 0.339]
$color->toOklch()->toArray(); // [0.3721, 0.1584, 307.45]
$color->toCmyk()->toArray(); // [0.252, 0.679, 0.0, 0.486]
// RGB packed integer (r << 16 | g << 8 | b)
$color->toInt(); // 6432387
// Accessibility helpers
$color->luminance(); // 0.042 (WCAG 2.x relative luminance)
$color->isDark(); // true
$color->textColor(); // RgbColor(255, 255, 255) — white text on this background
// Population data
$color->population(); // 4821 (number of pixels this color represents)
$color->proportion(); // 0.127 (fraction of analyzed pixels, 0-1)
$palette = $thief->getPalette('/path/to/image.jpg', colorCount: 6);
$palette->toHex('#'); // ["#622a83", "#d4a832", "#1e6b4a", ...]
$palette->toCss(); // ["rgb(98, 42, 131)", "rgb(212, 168, 50)", ...]
$palette->toArray(); // [[98,42,131], [212,168,50], ...]
$palette->toInt(); // [6432387, 13936178, ...]
// Convert entire palette to another colorspace
$hslPalette = $palette->toHsl();
$oklchPalette = $palette->toOklch();
// Chaining to convert to OKLCH colorspace and get CSS representation
$palette->toOklch()->toCss(); // ["oklch(0.4071 0.1471 310.39)", "oklch(0.7521 0.1381 87.12)", ...]
// map() and reduce() for custom processing
$hexList = $palette->map(fn ($color) => strtoupper($color->toHex('#')));
$thief = new ColorThief(quality: 10, minSaturation: 0.05);
// Faster extraction, keeping all other settings
$palette = $thief->with(quality: 50)->getPalette('/path/to/image.jpg');
// Stricter filtering for a single call
$color = $thief->with(whiteThreshold: 230, minSaturation: 0.1)->getColor('/path/to/image.jpg');
// Include near-white pixels (useful for images where white is meaningful)
$thief = new ColorThief(whiteThreshold: 255);
// Exclude semi-transparent pixels more aggressively
$thief = new ColorThief(alphaThreshold: 200);
// Discard gray and low-saturation pixels to focus on vivid colors
$thief = new ColorThief(minSaturation: 0.15);
use ColorThief\ImageRegion;
// Crop starting at (50, 100), 200 px wide and 150 px tall
$region = new ImageRegion(x: 50, y: 100, width: 200, height: 150);
// Or: from (50, 100) to the right/bottom edges of the image
$region = new ImageRegion(x: 50, y: 100);
$color = $thief->getColor('/path/to/image.jpg', region: $region);
$palette = $thief->getPalette('/path/to/image.jpg', region: $region);
new ColorThief(
int $quality = 10,
int $whiteThreshold = 250,
int $alphaThreshold = 125,
float $minSaturation = 0,
ColorSpace $colorSpace = ColorSpace::Oklch,
AdapterInterface|string|null $preferredAdapter = null,
)
new ImageRegion(int $x = 0, int $y = 0, ?int $width = null, ?int $height = null)
// Before (v2)
use ColorThief\ColorThief;
$color = ColorThief::getColor('/path/to/image.jpg');
$palette = ColorThief::getPalette('/path/to/image.jpg', 5);
// After (v3)
use ColorThief\ColorThief;
$thief = new ColorThief();
$color = $thief->getColor('/path/to/image.jpg');
$palette = $thief->getPalette('/path/to/image.jpg', colorCount: 5);
// Before (v2)
$color->getRed();
$color->getGreen();
$color->getBlue();
// After (v3)
$color->red();
$color->green();
$color->blue();
// Before (v2)
$color = ColorThief::getColor('https://example.com/image.jpg');
// After (v3)
// You are responsible for validating the URL and implementing
// appropriate security controls (allowlisting, redirect policy, timeouts, etc.)
$data = file_get_contents('https://example.com/image.jpg');
$color = $thief->getColor($data);
use ColorThief\ColorSpace;
$thief = new ColorThief(colorSpace: ColorSpace::Rgb);