capsulescodes/laravel-dominant-color

使用K-means聚类在图片中找到主色

0.3 2023-05-03 14:25 UTC

This package is auto-updated.

Last update: 2024-09-20 13:25:35 UTC


README

此laravel包是从naomi/php-dominantcolor存储库的分支创建的

此laravel包从图片中提取主色 - 这些颜色最有可能吸引观众的注意力。

Album arts example

该库使用K-means聚类方法将图像像素分组到不同的颜色桶中。然后根据饱和度和与其他颜色之间的差异评估这些颜色。从中选择两种颜色作为最突出的颜色(主色和次色)。

要求

  • PHP 8或更高版本
  • Laravel 8或更高版本

安装

通过运行以下命令将包安装到您的应用程序中

composer require capsulescodes/laravel-dominant-color

用法

我们想从图片heart.png中找到最突出的颜色以及三种额外的颜色。

您可以通过以下方式通过外观访问DominantColor

$imageSrc = "heart.png";
$totalNumberOfColors = 5; // primary + secondary + 3 additional

DominantColor::fromFile($imageSrc, $totalNumberOfColors);

颜色数量是颜色桶的数量,它还应包括主色和次色。

结果是以下结构的数组

$colorInfo = [
	'primary' => 0xDEFDEF,
	'secondary' => 0xABCABC,
	'palette' => [
		0 => ['color' => 0xABCABC, 'score' => 0.8],
		/// etc
	];

所有颜色都格式化为标准的RGB整数形式:0xRRGGBB

palette包含额外的颜色。`score`字段是次色选择算法的分数。所选次色的值为1.0

请参阅examples目录中的视觉演示。

此项目依赖于Benjamin Delespierre的优雅的PHP K-Means库。