bensquire/php-color-extractor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bensquire/php-color-extractor

Composer 安装命令:

composer require bensquire/php-color-extractor

包简介

A simple class to extract the primary colours from an image.

README 文档

README

Tests PHPStan PHP Version License

A modern PHP library to extract the dominant color palette from images.

Features

  • Extract dominant colors from any image format supported by GD
  • Configurable number of colors to extract
  • Adjustable granularity for performance vs. accuracy tradeoff
  • Fluent interface for easy chaining
  • Type-safe with strict PHP 8.3+ type declarations
  • Full PHPStan level max compliance
  • 100% test coverage

Requirements

  • PHP 8.3 or 8.4
  • GD extension

Installation

Install via Composer:

composer require bensquire/php-color-extractor

Usage

Basic Example

<?php

use PHPColorExtractor\PHPColorExtractor;

$extractor = new PHPColorExtractor();
$extractor->setImage('/path/to/image.jpg');
$palette = $extractor->extractPalette();

// Returns array of hex colors, e.g., ['CC6666', 'FF9933', '66CC99', ...]
print_r($palette);

Advanced Usage with Fluent Interface

<?php

use PHPColorExtractor\PHPColorExtractor;

$extractor = new PHPColorExtractor();
$palette = $extractor
    ->setImage('/path/to/image.jpg')
    ->setTotalColors(5)        // Extract 5 colors (default: 10)
    ->setGranularity(10)        // Check every 10th pixel (default: 5)
    ->extractPalette();

foreach ($palette as $color) {
    echo "#{$color}\n";
}

Configuration Options

setImage(string $image): self

Set the path to the image file to analyze.

Throws: Exception if the file doesn't exist.

setTotalColors(int $totalColors = 10): self

Set the number of dominant colors to extract.

Default: 10 Throws: Exception if the value is less than or equal to 0.

setGranularity(int $granularity = 5): self

Set the sampling granularity. Higher values = faster but less accurate. Lower values = slower but more accurate.

Default: 5 Throws: Exception if the value is less than or equal to 0.

extractPalette(): array<int, string>

Extract and return the color palette as an array of hex color strings.

Returns: Array of hex color strings (without # prefix) Throws: Exception if no image has been set or if image processing fails.

Examples

Web Color Palette Display

<?php

use PHPColorExtractor\PHPColorExtractor;

$extractor = new PHPColorExtractor();
$palette = $extractor
    ->setImage('uploaded-image.jpg')
    ->setTotalColors(8)
    ->extractPalette();

echo '<div class="palette">';
foreach ($palette as $color) {
    echo sprintf(
        '<div class="color" style="background-color: #%s;"></div>',
        $color
    );
}
echo '</div>';

Fast Preview (Low Accuracy)

<?php

use PHPColorExtractor\PHPColorExtractor;

// Quick extraction for thumbnails or previews
$palette = (new PHPColorExtractor())
    ->setImage('large-image.jpg')
    ->setTotalColors(3)
    ->setGranularity(20)  // Sample every 20th pixel for speed
    ->extractPalette();

High Accuracy Analysis

<?php

use PHPColorExtractor\PHPColorExtractor;

// Detailed extraction for color analysis
$palette = (new PHPColorExtractor())
    ->setImage('artwork.jpg')
    ->setTotalColors(15)
    ->setGranularity(1)   // Sample every pixel for maximum accuracy
    ->extractPalette();

Development

Running Tests

composer install
vendor/bin/phpunit

Running Static Analysis

vendor/bin/phpstan analyse

Running All Quality Checks

vendor/bin/phpstan analyse && vendor/bin/phpunit

How It Works

The library samples pixels from the image at intervals determined by the granularity setting. It then:

  1. Rounds RGB values to reduce similar colors
  2. Counts occurrences of each color
  3. Sorts by frequency
  4. Returns the most common colors

This approach is based on this Stack Overflow answer.

Contributing

Contributions are welcome! Please ensure:

  • All tests pass (vendor/bin/phpunit)
  • PHPStan analysis passes at max level (vendor/bin/phpstan analyse)
  • Code follows PSR-4 autoloading standards
  • New features include tests

License

This library is licensed under the MIT License. See LICENSE for details.

Credits

Changelog

See CHANGELOG.md for version history and changes.

bensquire/php-color-extractor 适用场景与选型建议

bensquire/php-color-extractor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 762 次下载、GitHub Stars 达 4, 最近一次更新时间为 2014 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「image」 「color」 「extract」 「colour」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bensquire/php-color-extractor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bensquire/php-color-extractor 我们能提供哪些服务?
定制开发 / 二次开发

基于 bensquire/php-color-extractor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 762
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-11