Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Color Thief Php Laravel Package

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.

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Setup
1. **Installation**:
   ```bash
   composer require ksubileau/color-thief-php

Ensure your server has GD, Imagick, or Gmagick installed (required for image processing).

  1. First Use Case: Extract the dominant color from an image (e.g., for dynamic UI themes or accent colors):

    use ColorThief\ColorThief;
    
    // Path to image (local or remote URL)
    $dominantColor = ColorThief::getColor('/path/to/image.jpg');
    // Returns: array([r, g, b]) e.g., [253, 42, 152]
    
  2. Key Files:

    • vendor/ksubileau/color-thief-php/src/ColorThief.php: Core class.
    • vendor/ksubileau/color-thief-php/tests/: Test cases for edge cases (e.g., corrupted images, CMYK).

Implementation Patterns

Core Workflows

  1. Dynamic Theming: Use getColor() to fetch a dominant color for CSS variables or UI elements:

    $hexColor = ColorThief::getColor($imagePath, quality: 5, outputFormat: 'hex');
    echo "<div style='background-color: {$hexColor};'>...</div>";
    
  2. Color Palettes: Generate a palette for design tools or filters:

    $palette = ColorThief::getPalette($imagePath, colorCount: 5);
    // Returns: array([r1,g1,b1], [r2,g2,b2], ...)
    
  3. Area-Specific Colors: Extract colors from a region (e.g., product images):

    $area = ['x' => 100, 'y' => 50, 'w' => 200, 'h' => 150];
    $localColor = ColorThief::getColor($imagePath, area: $area);
    
  4. Adapter Selection: Force a specific image processor (e.g., for performance or CMYK support):

    // Use Imagick (better for CMYK)
    $color = ColorThief::getColor($imagePath, adapter: 'Imagick');
    

Integration Tips

  • Laravel Blade: Cache results in a service provider to avoid reprocessing:

    // app/Providers/AppServiceProvider.php
    public function boot()
    {
        view()->composer('*', function ($view) {
            $view->with('dominantColor', ColorThief::getColor(storage_path('app/image.jpg')));
        });
    }
    
  • Queue Jobs: Offload heavy processing (e.g., palette generation) to a queue:

    // app/Jobs/GeneratePalette.php
    public function handle()
    {
        $palette = ColorThief::getPalette($this->imagePath, colorCount: 10);
        // Store in DB or cache
    }
    
  • Validation: Sanitize image paths/URLs to prevent SSRF or path traversal:

    use Illuminate\Support\Facades\Storage;
    
    $imagePath = Storage::path('uploads/' . $request->validated('image'));
    

Gotchas and Tips

Pitfalls

  1. Memory Limits:

    • High quality (e.g., 1) + large images may hit PHP’s memory_limit.
    • Fix: Start with quality: 5 and adjust based on server limits.
    • Monitor with memory_get_usage():
      $start = memory_get_usage();
      $color = ColorThief::getColor($imagePath, quality: 1);
      $usage = memory_get_usage() - $start; // Check if > 100MB
      
  2. Corrupted Images:

    • Throws RuntimeException if GD/Imagick fails to load.
    • Fix: Validate images before processing:
      try {
          $color = ColorThief::getColor($imagePath);
      } catch (\ColorThief\Exception\Exception $e) {
          Log::error("Invalid image: {$imagePath}");
          return response()->json(['error' => 'Invalid image'], 400);
      }
      
  3. CMYK Images:

    • Requires Imagick ≥ 3.0 or Gmagick.
    • Fix: Use adapter: 'Imagick' or ensure GD is compiled with CMYK support.
  4. Remote URLs:

    • May fail if allow_url_fopen is disabled.
    • Fix: Use file_get_contents() with a stream context or a library like Guzzle:
      $imageData = file_get_contents($remoteUrl, false, stream_context_create([
          'http' => ['timeout' => 10]
      ]));
      $color = ColorThief::getColor($imageData);
      
  5. Solid Color Images:

    • Returns the same color for all palette entries.
    • Fix: Add a fallback palette or check for uniformity:
      $palette = ColorThief::getPalette($imagePath, colorCount: 5);
      if (count(array_unique($palette)) === 1) {
          $palette = [/* fallback palette */];
      }
      

Debugging Tips

  • Log Adapter Choice: Override ColorThief to log which adapter is used:

    ColorThief::getColor($imagePath, adapter: 'Gd'); // Force GD for debugging
    
  • Profile Performance: Use microtime() to measure execution time:

    $start = microtime(true);
    $color = ColorThief::getColor($imagePath, quality: 1);
    $time = microtime(true) - $start; // Log if > 1s
    
  • Visual Validation: Compare results with the JavaScript Color Thief for consistency.

Extension Points

  1. Custom Adapters: Implement AdapterInterface for custom image sources (e.g., AWS S3):

    class S3Adapter implements AdapterInterface {
        public function load($source) { /* ... */ }
        public function getImageWidth($image) { /* ... */ }
        // ... other required methods
    }
    ColorThief::getColor($s3Path, adapter: new S3Adapter());
    
  2. Post-Processing: Use the Color object for advanced operations:

    $colorObj = ColorThief::getColor($imagePath, outputFormat: 'obj');
    $brightness = $colorObj->getBrightness(); // Custom method
    
  3. Caching: Cache results by image hash (e.g., md5(file_get_contents($path))):

    $cacheKey = 'color:' . md5($imagePath);
    $color = Cache::remember($cacheKey, now()->addHours(1), function () use ($imagePath) {
        return ColorThief::getColor($imagePath);
    });
    
  4. Testing: Mock the ColorThief class in unit tests:

    $mock = Mockery::mock('overload:' . ColorThief::class);
    $mock->shouldReceive('getColor')->andReturn([255, 0, 0]);
    

Configuration Quirks

  • PHP Extensions:

    • GD: Default fallback; may lack features (e.g., WebP support).
    • Imagick/Gmagick: Preferred for advanced formats (CMYK, WebP) but require CLI installation.
    • Check availability:
      if (!extension_loaded('gd') && !extension_loaded('imagick')) {
          throw new \RuntimeException('No image processing extensions found.');
      }
      
  • Output Formats:

    • hex is URL-safe (use for CSS/JS).
    • rgb is human-readable (e.g., rgb(255, 0, 0)).
    • obj enables custom methods (e.g., getHue()).
  • Quality vs. Speed:

    • Quality 1: High accuracy, slow (use for static assets).
    • Quality 10: Fast, lower accuracy (use for dynamic content).
    • Benchmark with ColorThief::getColor($imagePath, quality: X).

```markdown
### Example: Full Laravel Controller
```php
use ColorThief\ColorThief;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ImageController extends Controller
{
    public function getDominantColor(Request $request)
    {
        $imagePath = $request->validate(['image' => 'required|image'])->file('image')->store('temp');

        $cacheKey = 'color:' . md5($imagePath);
        $color = Cache::remember($cacheKey, now()->addMinutes(30), function () use ($image
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor