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.
request()->file('image')->get()).dispatch(new ExtractColorsJob($imagePath))).| Risk Area | Mitigation Strategy |
|---|---|
| Memory Limits | High-quality settings ($quality=1) may exceed memory_limit. Solution: Default to $quality=10 and document scaling strategies (e.g., chunked processing for large images). |
| Extension Dependencies | Requires GD/Imagick/Gmagick. Solution: Validate extensions in composer.json or Laravel’s bootstrap/app.php. Fallback to a lower-quality mode if unavailable. |
| Remote Image Failures | URLs may fail due to network issues or invalid paths. Solution: Wrap calls in try-catch and implement retries (e.g., Laravel’s retry() helper). |
| WebP Support | Requires Imagick/Gmagick ≥3.0 for full WebP support. Solution: Graceful degradation for unsupported formats. |
| Performance | Palette generation can be CPU-intensive. Solution: Offload to queues (e.g., ColorThief::getPalette() in a HandleColors job). |
$quality; batch allows higher quality.adapter parameter defaults and error handling.hex) or allow flexibility?hex for CSS) but reduces reuse options.Storage::disk() for local/S3 image paths.ExtractColorsJob).@colorPalette($imagePath, 5)).color:extract command for batch processing.composer.json:
"require": {
"ext-gd": "*",
"ext-imagick": "*"
},
"conflict": {
"ext-gmagick": ">=1.0"
}
Phase 1: Proof of Concept
composer require ksubileau/color-thief-php.use ColorThief\ColorThief;
$hexColor = ColorThief::getColor(storage_path('app/image.jpg'), 10, null, 'hex');
hex, rgb, array) against UI requirements.Phase 2: Integration
ColorThiefService to centralize configuration (e.g., default adapter, quality):
$this->app->singleton(ColorThiefService::class, function ($app) {
return new ColorThiefService(config('color-thief.quality', 10), config('color-thief.adapter', null));
});
ColorThief facade for Blade/Controller access:
facade(ColorThief::class, ColorThiefService::class);
Illuminate\Bus\Queueable for async processing:
class ExtractColorsJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable;
public function handle() {
$palette = ColorThief::getPalette($this->imagePath, $this->colorCount);
// Store in DB/cache
}
}
Phase 3: Optimization
Cache::remember()).chunk() for large datasets.| Component | Compatibility Notes |
|---|---|
| Laravel Versions | Tested with Laravel 8+ (PHP 7.2+). For Laravel 7, use v1.x of the package. |
| PHP Extensions | Prioritize Imagick for WebP/CMYK; GD as fallback. |
| Image Formats | Supports JPEG, PNG, GIF, WebP. CMYK requires Imagick ≥3.0. |
| Storage Systems | Works with local files, S3 (via Laravel Filesystem), and binary strings. |
php -m | grep -E 'gd|imagick').config/color-thief.php:
return [
'adapter' => env('COLOR_THIEF_ADAPTER', 'imagick'), // 'gd', 'gmagick', or null (auto)
'quality' => 10,
'fallback_color' => '#000000', // Default if extraction fails
];
memory_get_usage() in logs).composer.json to avoid unexpected updates:
"ksubileau/color-thief-php": "^2.0"
memory_get_usage() before/after).ColorThiefExceptionHandler:
try {
$color = ColorThief::getColor($image);
} catch (ColorThief\Exception\Exception $e) {
Log::error("Color extraction failed: {$e->getMessage()}");
return config('color-thief.fallback_color');
}
How can I help you explore Laravel packages today?