{"id":36333478,"url":"https://github.com/bensquire/php-color-extractor","last_synced_at":"2026-03-01T01:03:56.889Z","repository":{"id":14010761,"uuid":"16712267","full_name":"bensquire/php-color-extractor","owner":"bensquire","description":"A simple library to extract the palette of an image.","archived":false,"fork":false,"pushed_at":"2025-11-08T21:58:37.000Z","size":228,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-11T15:24:15.920Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bensquire.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2014-02-10T22:56:54.000Z","updated_at":"2025-11-08T21:58:41.000Z","dependencies_parsed_at":"2022-08-24T12:50:50.727Z","dependency_job_id":null,"html_url":"https://github.com/bensquire/php-color-extractor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bensquire/php-color-extractor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bensquire%2Fphp-color-extractor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bensquire%2Fphp-color-extractor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bensquire%2Fphp-color-extractor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bensquire%2Fphp-color-extractor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bensquire","download_url":"https://codeload.github.com/bensquire/php-color-extractor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bensquire%2Fphp-color-extractor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29957128,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T22:53:01.873Z","status":"ssl_error","status_checked_at":"2026-02-28T22:52:50.699Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-11T12:02:18.634Z","updated_at":"2026-03-01T01:03:51.859Z","avatar_url":"https://github.com/bensquire.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Color Extractor\n\n[![Tests](https://github.com/bensquire/php-color-extractor/workflows/Tests/badge.svg)](https://github.com/bensquire/php-color-extractor/actions)\n[![PHPStan](https://img.shields.io/badge/PHPStan-level%20max-brightgreen.svg)](https://phpstan.org/)\n[![PHP Version](https://img.shields.io/badge/php-%5E8.3-blue.svg)](https://php.net)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nA modern PHP library to extract the dominant color palette from images.\n\n## Features\n\n- Extract dominant colors from any image format supported by GD\n- Configurable number of colors to extract\n- Adjustable granularity for performance vs. accuracy tradeoff\n- Fluent interface for easy chaining\n- Type-safe with strict PHP 8.3+ type declarations\n- Full PHPStan level max compliance\n- 100% test coverage\n\n## Requirements\n\n- PHP 8.3 or 8.4\n- GD extension\n\n## Installation\n\nInstall via Composer:\n\n```bash\ncomposer require bensquire/php-color-extractor\n```\n\n## Usage\n\n### Basic Example\n\n```php\n\u003c?php\n\nuse PHPColorExtractor\\PHPColorExtractor;\n\n$extractor = new PHPColorExtractor();\n$extractor-\u003esetImage('/path/to/image.jpg');\n$palette = $extractor-\u003eextractPalette();\n\n// Returns array of hex colors, e.g., ['CC6666', 'FF9933', '66CC99', ...]\nprint_r($palette);\n```\n\n### Advanced Usage with Fluent Interface\n\n```php\n\u003c?php\n\nuse PHPColorExtractor\\PHPColorExtractor;\n\n$extractor = new PHPColorExtractor();\n$palette = $extractor\n    -\u003esetImage('/path/to/image.jpg')\n    -\u003esetTotalColors(5)        // Extract 5 colors (default: 10)\n    -\u003esetGranularity(10)        // Check every 10th pixel (default: 5)\n    -\u003eextractPalette();\n\nforeach ($palette as $color) {\n    echo \"#{$color}\\n\";\n}\n```\n\n### Configuration Options\n\n#### `setImage(string $image): self`\n\nSet the path to the image file to analyze.\n\n**Throws:** `Exception` if the file doesn't exist.\n\n#### `setTotalColors(int $totalColors = 10): self`\n\nSet the number of dominant colors to extract.\n\n**Default:** 10\n**Throws:** `Exception` if the value is less than or equal to 0.\n\n#### `setGranularity(int $granularity = 5): self`\n\nSet the sampling granularity. Higher values = faster but less accurate. Lower values = slower but more accurate.\n\n**Default:** 5\n**Throws:** `Exception` if the value is less than or equal to 0.\n\n#### `extractPalette(): array\u003cint, string\u003e`\n\nExtract and return the color palette as an array of hex color strings.\n\n**Returns:** Array of hex color strings (without # prefix)\n**Throws:** `Exception` if no image has been set or if image processing fails.\n\n## Examples\n\n### Web Color Palette Display\n\n```php\n\u003c?php\n\nuse PHPColorExtractor\\PHPColorExtractor;\n\n$extractor = new PHPColorExtractor();\n$palette = $extractor\n    -\u003esetImage('uploaded-image.jpg')\n    -\u003esetTotalColors(8)\n    -\u003eextractPalette();\n\necho '\u003cdiv class=\"palette\"\u003e';\nforeach ($palette as $color) {\n    echo sprintf(\n        '\u003cdiv class=\"color\" style=\"background-color: #%s;\"\u003e\u003c/div\u003e',\n        $color\n    );\n}\necho '\u003c/div\u003e';\n```\n\n### Fast Preview (Low Accuracy)\n\n```php\n\u003c?php\n\nuse PHPColorExtractor\\PHPColorExtractor;\n\n// Quick extraction for thumbnails or previews\n$palette = (new PHPColorExtractor())\n    -\u003esetImage('large-image.jpg')\n    -\u003esetTotalColors(3)\n    -\u003esetGranularity(20)  // Sample every 20th pixel for speed\n    -\u003eextractPalette();\n```\n\n### High Accuracy Analysis\n\n```php\n\u003c?php\n\nuse PHPColorExtractor\\PHPColorExtractor;\n\n// Detailed extraction for color analysis\n$palette = (new PHPColorExtractor())\n    -\u003esetImage('artwork.jpg')\n    -\u003esetTotalColors(15)\n    -\u003esetGranularity(1)   // Sample every pixel for maximum accuracy\n    -\u003eextractPalette();\n```\n\n## Development\n\n### Running Tests\n\n```bash\ncomposer install\nvendor/bin/phpunit\n```\n\n### Running Static Analysis\n\n```bash\nvendor/bin/phpstan analyse\n```\n\n### Running All Quality Checks\n\n```bash\nvendor/bin/phpstan analyse \u0026\u0026 vendor/bin/phpunit\n```\n\n## How It Works\n\nThe library samples pixels from the image at intervals determined by the granularity setting. It then:\n\n1. Rounds RGB values to reduce similar colors\n2. Counts occurrences of each color\n3. Sorts by frequency\n4. Returns the most common colors\n\nThis approach is based on [this Stack Overflow answer](http://stackoverflow.com/questions/3468500/detect-overall-average-color-of-the-picture?answertab=active#tab-top).\n\n## Contributing\n\nContributions are welcome! Please ensure:\n\n- All tests pass (`vendor/bin/phpunit`)\n- PHPStan analysis passes at max level (`vendor/bin/phpstan analyse`)\n- Code follows PSR-4 autoloading standards\n- New features include tests\n\n## License\n\nThis library is licensed under the MIT License. See [LICENSE](LICENSE) for details.\n\n## Credits\n\n- [Ben Squire](https://github.com/bensquire) - Original author\n- Inspired by [Stack Overflow community](http://stackoverflow.com/questions/3468500/)\n\n## Changelog\n\nSee [CHANGELOG.md](CHANGELOG.md) for version history and changes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbensquire%2Fphp-color-extractor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbensquire%2Fphp-color-extractor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbensquire%2Fphp-color-extractor/lists"}