composer require ksubileau/color-thief-php
{
"require": {
"ksubileau/color-thief-php": "^1.4"
},
// autoload 加载自定义修改后类 的方法,来替换vendor中的类 的方法,
// 只修改要改类 的方法,其他 与 vendor中的类一致
"autoload": {
"classmap": [
"ColorThief/CMap.php"
]
}
}
public function palette()
{
$total = array_sum($this->vboxes->map(function ($x) {
return $x['vbox']->count();
}));
return $this->vboxes->map(function ($vb) use ($total) {
return array(
$vb['color'],
(int)($vb['vbox']->count() / (float)$total * 100)
);
});
}
// 注释为原来的代码
/*public function palette()
{
return $this->vboxes->map(function ($vb) {
return $vb['color'];
});
}*/
composer install
composer dump-autoload
// 图片路径
$tempPicPath = RUNTIME_PATH . 'test.jpg';
// 色值top?
$colorCount = 3;
// 提取质量1最高
$colorQuality = 1;
// 开始提取
$palette = ColorThief::getPalette($tempPicPath, $colorCount, $colorQuality);
print_r($palette);
// RGB 转为 颜色值(#FF0011)
function rgbToColor($R, $G = -1, $B = -1)
{
if (is_array($R) && sizeof($R) == 3) {
list($R, $G, $B) = $R;
}
$R = intval($R);
$G = intval($G);
$B = intval($B);
$R = dechex($R < 0 ? 0 : ($R > 255 ? 255 : $R));
$G = dechex($G < 0 ? 0 : ($G > 255 ? 255 : $G));
$B = dechex($B < 0 ? 0 : ($B > 255 ? 255 : $B));
$COLOR = (strlen($R) < 2 ? '0' : '') . $R;
$COLOR .= (strlen($G) < 2 ? '0' : '') . $G;
$COLOR .= (strlen($B) < 2 ? '0' : '') . $B;
return '#' . $COLOR;
}
https://blog.csdn.net/yssong1028/article/details/92829334
https://segmentfault.com/q/1010000007298980