PHP code example of abderrahimghazali / color-palette-extractor

1. Go to this page and download the library: Download abderrahimghazali/color-palette-extractor 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/ */

    

abderrahimghazali / color-palette-extractor example snippets


use ColorPaletteExtractor\ColorPalette;

// Extract 5 dominant colors
$palette = ColorPalette::fromImage('path/to/image.jpg');
$colors = $palette->getDominant(5);
// ['#ff5733', '#33ff57', '#3357ff', '#ffff33', '#ff33ff']

// Get primary color
$primary = $palette->getPrimary();
// '#ff5733'

// With percentages
$detailed = $palette->getPaletteWithPercentages(3);
// [
//   ['color' => '#ff5733', 'percentage' => 35.2],
//   ['color' => '#33ff57', 'percentage' => 28.1],
//   ['color' => '#3357ff', 'percentage' => 15.7]
// ]

// RGB format
$rgbColors = ColorPalette::fromImage('image.jpg')
    ->format('rgb')
    ->getDominant(3);
// [[255, 87, 51], [51, 255, 87], [51, 87, 255]]

// HSL format  
$hslColors = ColorPalette::fromImage('image.jpg')
    ->format('hsl')
    ->getDominant(3);
// [[9, 100, 60], [129, 100, 60], [219, 100, 60]]

// Higher quality = slower but more accurate
$palette = ColorPalette::fromImage('image.jpg')
    ->quality(8) // 1-10 scale
    ->getDominant(5);

// Lower quality = faster processing
$fastPalette = ColorPalette::fromImage('large-image.jpg')
    ->quality(3)
    ->getDominant(5);

$palette = ColorPalette::fromImage('image.jpg');

// Check if image is light or dark
$brightness = $palette->getBrightness();
// 'light' or 'dark'

// Get complementary colors
$complementary = $palette->getComplementary(3);
// Colors that complement the dominant ones

// From raw image data
$imageData = file_get_contents('https://example.com/image.jpg');
$palette = ColorPalette::fromData($imageData);
$colors = $palette->getDominant(5);

// From uploaded file
$palette = ColorPalette::fromData($_FILES['image']['tmp_name']);

function generateTheme($logoPath) {
    $palette = ColorPalette::fromImage($logoPath);
    
    return [
        'primary' => $palette->getPrimary(),
        'secondary' => $palette->getDominant(2)[1] ?? '#cccccc',
        'accent' => $palette->getComplementary(1)[0],
        'brightness' => $palette->getBrightness()
    ];
}

function analyzeProductImage($imagePath) {
    $palette = ColorPalette::fromImage($imagePath)
        ->quality(7); // High quality for product images
    
    $analysis = $palette->getPaletteWithPercentages(5);
    
    return [
        'dominant_colors' => $analysis,
        'is_colorful' => count($analysis) > 3,
        'brightness' => $palette->getBrightness(),
        'primary_color' => $palette->getPrimary()
    ];
}

function generateColorTags($imagePath) {
    $colors = ColorPalette::fromImage($imagePath)
        ->format('hsl')
        ->getDominant(3);
    
    $tags = [];
    foreach ($colors as $hsl) {
        $hue = $hsl[0];
        if ($hue < 30) $tags[] = 'red';
        elseif ($hue < 90) $tags[] = 'yellow';
        elseif ($hue < 150) $tags[] = 'green';
        elseif ($hue < 210) $tags[] = 'cyan';
        elseif ($hue < 270) $tags[] = 'blue';
        elseif ($hue < 330) $tags[] = 'magenta';
        else $tags[] = 'red';
    }
    
    return array_unique($tags);
}

try {
    $palette = ColorPalette::fromImage('nonexistent.jpg');
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

// Common exceptions:
// - "GD extension is