Download the PHP package farzai/color-palette without Composer
On this page you can find all versions of the php package farzai/color-palette. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download farzai/color-palette
More information about farzai/color-palette
Files in farzai/color-palette
Package color-palette
Short Description A robust PHP library for extracting, analyzing, and managing color palettes from images
License MIT
Homepage https://github.com/parsilver/color-palette-php
Informations about the package color-palette
Color Palette PHP
A powerful PHP library for extracting color palettes from images and generating color themes. This package supports multiple image processing backends (GD and Imagick) and provides a rich set of color manipulation features.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Core Concepts
- Common Use Cases
- Generate Website Theme from Logo
- Create Accessible Color Scheme
- Extract Colors to CSS Variables
- Find Complementary Colors
- API Reference
- Working with Images
- Extracting Color Palettes
- Color Format Conversions
- Color Manipulation
- Color Analysis
- Theme Generation
- Troubleshooting & FAQ
- Testing
- Contributing
- License
Features
- Extract dominant colors from images using advanced color quantization
- Support for multiple image formats (JPEG, PNG, GIF, etc.)
- Multiple image processing backends (GD and Imagick)
- Generate color themes with surface, background, and accent colors
- Color manipulation with RGB, HSL, HSV, CMYK, and LAB support
- Color contrast ratio calculations (WCAG compliance)
- Automatic text color suggestions for optimal readability
- Smart surface color recommendations based on color brightness
- Deterministic color extraction - same image always produces same results
- Immutable color objects - safe and predictable
- Memory efficient with support for large images
Requirements
- PHP 8.2 or higher
- GD extension OR ImageMagick extension (at least one is required)
- A PSR-18 HTTP client only if you load images from URLs (e.g.
symfony/http-clientorguzzlehttp/guzzle) — not needed for local files - Composer
Installation
Install the package via composer:
Loading images from URLs (optional)
Extracting colors from local files has no HTTP dependencies. To load images from remote URLs, also install any PSR-18 HTTP client — it is auto-discovered, no wiring required:
symfony/http-client is recommended because the loader configures it to not follow
redirects, so every redirect hop is re-validated against the SSRF rules. You can also
pass your own PSR-18 client / PSR-17 factory to ImageLoaderFactory or ImageLoader.
Quick Start
Simple Approach (Recommended)
Extract colors in one line:
Builder Pattern (Flexible)
Chain multiple operations with the builder:
Advanced (Full Control)
Use individual components for dependency injection and custom configuration:
Core Concepts
ImageFactory
Load images from files for color extraction.
ColorExtractor
Extract dominant colors using color quantization. Two drivers available:
- GD - Built into most PHP installations
- Imagick - More accurate, better for complex images
ColorPalette
A collection of extracted colors with array-like access.
Color Objects
Immutable objects supporting multiple color spaces.
Note: Color objects are immutable - methods always return new instances.
Common Use Cases
1. Generate Website Theme from Logo
Extract colors from your logo and create a complete theme:
2. Create Accessible Color Scheme (WCAG Compliant)
Ensure your color combinations meet accessibility standards:
3. Generate CSS Variables from Image
Create CSS custom properties from image colors:
4. Generate Color Schemes
Create color harmonies using built-in schemes:
API Reference
Working with Images
Simple: ColorPalette::fromImage()
Parameters:
$path(string) - Path to image file$count(int) - Number of colors to extract (default: 5)$driver(string) - 'gd' or 'imagick' (default: 'gd')
Returns: ColorPalette
Advanced: ImageFactory
Extracting Color Palettes
Simple: ColorPalette::fromImage()
Advanced: Using ColorExtractor
Color Format Conversions
The Color class supports multiple color space conversions:
Creating Colors
Converting Colors
Color Manipulation
All manipulation methods return new Color instances (immutable):
Color Analysis
WCAG Contrast Requirements:
- AAA Normal Text: 7:1 or higher
- AA Normal Text: 4.5:1 or higher
- AA Large Text: 3:1 or higher
Theme Generation
ColorPalette::getSuggestedSurfaceColors()
Get a complete color theme from the palette:
Returns: Array with keys:
'surface'- Lightest color (main surface)'background'- Second lightest (secondary backgrounds)'accent'- Accent color with good contrast'surface_variant'- Variant of surface color
This is a lightweight heuristic helper that returns a plain array. For a typed, validated 5-role theme, use
Theme(below).
Typed themes — Theme
Theme is a value object that always defines the five roles primary,
secondary, accent, background, surface; its getters never throw.
ColorPalette::getSuggestedTextColor()
Get optimal text color (black or white) for a background:
Parameters:
$backgroundColor(ColorInterface) - Background color
Returns: ColorInterface - Either black (#000000) or white (#ffffff) based on contrast
Troubleshooting & FAQ
Which image processing extension should I use?
GD (Recommended for most users):
- Pre-installed in most PHP environments
- Good color extraction quality
- Lower memory usage
- Slightly less accurate than Imagick
Imagick (For better accuracy):
- More accurate color extraction
- Better handling of complex images
- Supports more image formats
- Requires additional installation
- Higher memory usage
How do I check which extension is installed?
Or use the built-in checker:
How do I install GD or Imagick?
Ubuntu/Debian:
macOS (Homebrew):
Restart your web server after installation!
The colors extracted don't look right
-
Try the other driver - Imagick often produces more accurate results:
-
Extract more colors - Increase the count for better variety:
-
Check image quality - Low quality or heavily compressed images may produce poor results
- Image has many similar colors - This is expected behavior; the library finds dominant colors
Memory issues with large images
For very large images (> 5MB), consider:
- Resize before processing (using GD or Imagick directly)
-
Increase PHP memory limit in php.ini:
- Use GD instead of Imagick (lower memory usage)
Supported image formats
GD Driver:
- JPEG/JPG
- PNG
- GIF
- WebP (PHP 7.1+)
Imagick Driver:
- All of the above plus:
- TIFF
- BMP
- PSD
- And many more
Why are my Color objects not changing?
Color objects are immutable. Methods return new instances:
How many colors should I extract?
- 5-8 colors - Good for general use, themes
- 10-15 colors - More variety, better for finding specific shades
- 3-5 colors - Minimal, quick extraction
More colors = more processing time and memory usage.
Security
HTTP Client Security
The library implements comprehensive security measures when loading images from URLs to protect against SSRF (Server-Side Request Forgery) and other attacks.
SSRF Protection
All remote URLs are validated before making HTTP requests:
Blocked Protocols:
- Only
http://andhttps://are allowed - All other protocols (
file://,ftp://,gopher://, etc.) are rejected
Blocked IP Addresses:
- Localhost:
127.0.0.1,::1 - Private networks:
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16 - Link-local:
169.254.0.0/16,fe80::/10 - Unique local (IPv6):
fc00::/7,fd00::/8 - IPv4-mapped IPv6 addresses:
::ffff:0:0/96
Choosing an HTTP Client (redirect safety)
The loader follows redirects itself and re-validates every hop against the
SSRF rules above — but only if the underlying PSR-18 client does not follow
redirects on its own. The default does this correctly: when symfony/http-client
is installed, the factory configures it with max_redirects = 0.
⚠️ A different PSR-18 client (e.g. Guzzle) may follow redirects internally by default, which bypasses the per-hop re-validation — a redirect from a public URL to an internal address would be followed before the loader can check it. When accepting user-supplied URLs, prefer
symfony/http-client, or inject a client configured not to follow redirects:
File Size Limits
Downloaded files are limited to 10MB by default to prevent denial-of-service attacks:
Files are streamed in chunks (8KB at a time) and validated during download, not loaded entirely into memory.
MIME Type Validation
Remote files are validated to ensure they're actual images:
- Content-Type Header: Checked if present in HTTP response
- File Detection: Actual file content verified using
finfoafter download
Only these MIME types are accepted:
image/jpeg,image/jpgimage/pngimage/gifimage/webpimage/bmpimage/tiff
HTTP Client Configuration
Customize HTTP client behavior for security and performance:
Security Recommendations:
- Keep
maxRedirects: 0- Redirects can bypass SSRF protection - Keep
verifySsl: true- Prevents man-in-the-middle attacks - Set appropriate timeout - Prevents hanging on slow servers
- Limit file size - Protects against DoS via large downloads
Exception Handling
Different exception types for different security scenarios:
Best Practices for User-Provided URLs
When accepting URLs from users:
Testing
Run the test suite:
Run tests with coverage:
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of color-palette with dependencies
php-http/discovery Version ^1.20
psr/http-client Version ^1.0
psr/http-factory Version ^1.1
psr/http-message Version ^2.0
psr/log Version ^1.1|^2.0|^3.0