1. Go to this page and download the library: Download farzai/color-palette 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/ */
use Farzai\ColorPalette\ColorPalette;
use Farzai\ColorPalette\Color;
// Extract from image
$palette = ColorPalette::builder()
->fromImage('path/to/image.jpg')
->withCount(8)
->build();
// Generate from a base color
$palette = ColorPalette::builder()
->withBaseColor(Color::fromHex('#3498db'))
->withScheme('monochromatic', ['count' => 7])
->build();
use Farzai\ColorPalette\ImageFactory;
use Farzai\ColorPalette\ColorExtractorFactory;
// Static methods
$image = ImageFactory::fromPath('path/to/image.jpg');
$extractor = ColorExtractorFactory::gd();
$palette = $extractor->extract($image, 5);
// Instance methods with dependencies
$extractor = (new ColorExtractorFactory($logger))->make('gd');
$palette = $extractor->extract($image, 5);
$color = Color::fromHex('#3498db');
// Lighten and darken
$lighter = $color->lighten(0.2); // Lighten by 20% (0.0 to 1.0)
$darker = $color->darken(0.2); // Darken by 20% (0.0 to 1.0)
// Adjust saturation
$saturated = $color->saturate(0.3); // Increase saturation by 30%
$desaturated = $color->desaturate(0.3); // Decrease saturation by 30%
// Rotate hue (color wheel)
$rotated = $color->rotate(180); // Rotate hue by 180 degrees (0-360)
// Set specific lightness
$withLightness = $color->withLightness(0.5); // Set lightness to 50% (0.0 to 1.0)
$color = Color::fromHex('#3498db');
// Brightness (0-255)
$brightness = $color->getBrightness(); // Returns: float (0.0 to 255.0)
$isLight = $color->isLight(); // true if brightness > 128
$isDark = $color->isDark(); // true if brightness <= 128
// Luminance (0.0 to 1.0) - WCAG formula
$luminance = $color->getLuminance(); // Returns: float (0.0 to 1.0)
// Contrast ratio (for accessibility)
$textColor = Color::fromHex('#ffffff');
$ratio = $color->getContrastRatio($textColor); // Returns: float (1.0 to 21.0)
$theme = $palette->getSuggestedSurfaceColors();
use Farzai\ColorPalette\Color;
use Farzai\ColorPalette\ColorPalette;
use Farzai\ColorPalette\PaletteGenerator;
use Farzai\ColorPalette\Theme;
use Farzai\ColorPalette\ThemeGenerator;
// Full 5-role theme from a brand color (via the website-theme strategy):
$palette = (new PaletteGenerator(Color::fromHex('#2196F3')))->websiteTheme();
$theme = Theme::fromPalette($palette);
echo $theme->getPrimaryColor()->toHex();
echo $theme->getSurfaceColor()->toHex();
// Or derive a theme from any extracted palette:
$theme = (new ThemeGenerator)->generate(ColorPalette::fromImage('logo.png', 5));
// Check for GD
if (extension_loaded('gd')) {
echo "GD is installed\n";
}
// Check for Imagick
if (extension_loaded('imagick')) {
echo "Imagick is installed\n";
}
try {
$extractor = ColorExtractorFactory::gd();
echo "GD is available\n";
} catch (\RuntimeException $e) {
echo "GD is not available\n";
}
$color = Color::fromHex('#3498db');
// WRONG - This doesn't work!
$color->lighten(0.2);
echo $color->toHex(); // Still #3498db
// CORRECT - Assign the result
$lighter = $color->lighten(0.2);
echo $lighter->toHex(); // Lighter color!
use Farzai\ColorPalette\ImageLoaderFactory;
$factory = new ImageLoaderFactory;
$loader = $factory->create();
// These will throw SsrfException:
$loader->load('http://localhost/internal-image.jpg'); // Blocked
$loader->load('http://192.168.1.1/image.jpg'); // Blocked
$loader->load('http://[::1]/image.jpg'); // Blocked
$loader->load('file:///etc/passwd'); // Blocked
// Only public HTTP/HTTPS URLs are allowed:
$loader->load('https://example.com/public-image.jpg'); // OK
> // Guzzle: disable redirect-following so the loader re-validates each hop
> $client = new \GuzzleHttp\Client(['allow_redirects' => false]);
> $loader = (new \Farzai\ColorPalette\ImageLoaderFactory(httpClient: $client))->create();
>
use Farzai\ColorPalette\Config\HttpClientConfig;
use Farzai\ColorPalette\ImageLoaderFactory;
// Custom file size limit (5MB)
$config = new HttpClientConfig(
maxFileSizeBytes: 5 * 1024 * 1024
);
$factory = new ImageLoaderFactory(httpConfig: $config);
$loader = $factory->create();
// Will throw HttpException if file exceeds 5MB
$loader->load('https://example.com/huge-image.jpg');
use Farzai\ColorPalette\Config\HttpClientConfig;
use Farzai\ColorPalette\ImageLoaderFactory;
$config = new HttpClientConfig(
timeoutSeconds: 30, // Request timeout (default: 30)
maxRedirects: 0, // Follow redirects (default: 0 - disabled)
maxFileSizeBytes: 10485760, // Max file size in bytes (default: 10MB)
userAgent: 'MyApp/1.0', // Custom User-Agent header
verifySsl: true // SSL certificate verification (default: true)
);
$factory = new ImageLoaderFactory(httpConfig: $config);
$loader = $factory->create();
use Farzai\ColorPalette\ColorExtractorFactory;
use Farzai\ColorPalette\ImageLoaderFactory;
// ✓ GOOD: load through the SSRF-protected loader and handle errors.
// (ColorPalette::fromImage() is for local paths only — it does not fetch URLs.)
try {
$image = (new ImageLoaderFactory)->create()->load($userUrl);
$palette = (new ColorExtractorFactory)->make('gd')->extract($image, 5);
} catch (SsrfException $e) {
// Don't expose internal network structure in error messages
throw new UserFacingException('Invalid URL provided');
}
// ⚠️ The loader already validates the URL against the SSRF rules, but for
// untrusted input add an allowlist for defense in depth (see below).
$image = (new ImageLoaderFactory)->create()->load($_GET['url']);
// ✓ GOOD: Add URL whitelist for extra security
$allowedDomains = ['cdn.example.com', 'images.example.com'];
$host = parse_url($userUrl, PHP_URL_HOST);
if (!in_array($host, $allowedDomains)) {
throw new Exception('URL not from allowed domain');
}