给一张图片,获取图片中的主要颜色值RGB值
php
// 获取图片的主要颜色值RGB
public function maincolor()
{
$image = 'E:/phpstudy_pro/WWW/test/public/uploads/33.jpg';
$rTotal = $gTotal = $bTotal = $total = 0;
$i = imagecreatefromjpeg($image);
for ($x = 0; $x < imagesx($i); $x++) {
for ($y = 0; $y < imagesy($i); $y++) {
$rgb = imagecolorat($i, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$rTotal += $r;
$gTotal += $g;
$bTotal += $b;
$total++;
}
}
$rAverage = round($rTotal / $total);
$gAverage = round($gTotal / $total);
$bAverage = round($bTotal / $total);
$arr = array(
'r' => $rAverage,
'g' => $gAverage,
'b' => $bAverage,
);
echo "<pre>";
print_r($arr);
echo "</pre>";
}
输入图片和返回结果