11 08 2015

7. 确定任意图片的主导颜色

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function dominant_color($image)
{
$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 >>  & 0xFF;
        $b   $rgb & 0xFF;
        $rTotal += $r;
        $gTotal += $g;
        $bTotal += $b;
        $total++;
    }
}
$rAverage round($rTotal/$total);
$gAverage round($gTotal/$total);
$bAverage round($bTotal/$total);
}
发表评论