nikkanetiya/laravel-color-palette

此包的最新版本(1.3.0)没有可用的许可证信息。

Laravel 对 `ksubileau/color-thief-php` 的包装。从图像中抓取主色或代表色板。使用 PHP 和 GD 或 Imagick。

1.3.0 2019-04-24 02:17 UTC

This package is auto-updated.

Last update: 2024-09-25 17:46:11 UTC


README

Laravel 对 Color-Thief-PHP 的包装,并进行了一些额外修改。从图像中抓取 主色代表色板。使用 PHP 和 GD 或 Imagick。

此 Laravel 包非常适合从图像中抓取 主色代表色板。请参考此示例图像。

example image

内容

安装

您可以通过 Composer 安装此包

composer require nikkanetiya/laravel-color-palette

您必须安装服务提供者(对于 Laravel < 5.5)

// config/app.php
'providers' => [
    ...
    NikKanetiya\LaravelColorPalette\ColorPaletteServiceProvider::class,
],

注册外观

// config/app.php
'aliases' => [
    ...
    'ColorPalette' => NikKanetiya\LaravelColorPalette\ColorPaletteFacade::class,
],

可用方法

  1. getColor() - 使用此方法从图像获取 最显眼的单色

    示例

    // get most dominant color from image
    
    $color = ColorPalette::getColor( 'https://rawcdn.githack.com/nikkanetiya/laravel-color-palette/master/tests/images/strawberry.jpeg' );
    
    // Color provides several getters/properties
    echo $color;             // '#dc5550'
    echo $color->rgbString;  // 'rgb(220,85,80)'
    echo $color->rgbaString; // 'rgba(220,85,80,1)'
    echo $color->int;        // 14439760
    print_r($color->rgb);        // array(220, 85, 80) 
    print_r($color->rgba);       // array(220, 85, 80, 1)

    选项

    $color = ColorPalette::getColor($sourceImage, $quality = 10, $area = null );

    默认情况下,getColor 将有质量 -> 10 和特定区域 -> null。

    • 质量 可以是整数。1 是最高质量。质量和速度之间存在权衡。数字越大,色板生成速度越快,但漏掉颜色的可能性也越大。
    • 区域 可以是数组|null $area[x,y,w,h]。它允许您指定图像中的矩形区域,以便只为该区域获取颜色。它需要一个具有以下键的关联数组
      • $area['x']: 区域左上角的 x 坐标。默认为 0。
      • $area['y']: 区域左上角的 y 坐标。默认为 0。
      • $area['w']: 区域的宽度。默认为图像宽度减去 x 坐标。
      • $area['h']: 区域的高度。默认为图像高度减去 y 坐标。
  2. getPalette() - 使用此方法从图像中查找 代表色板

    示例

    // get colors from image
    
    $colors = ColorPalette::getPalette( 'https://github.com/nikkanetiya/laravel-color-palette/blob/master/tests/images/strawberry.jpeg' );
    
    foreach($colors as $color) {
        //
    }
    // Colors will be array of Color Objects

    选项

    $color = ColorPalette::getPalette($sourceImage, $colorCount = 10, $quality = 10, $area = null)
    • colorCount 可以是 2 到 256。这是您想要从图像中检索的颜色数量。
    • 质量区域 与上面相同。

图像来源: https://www.pexels.com, google image