php 手册中有如下说明: imagecolorat (php 4, php 5) imagecolorat get the index of the color of a pixel ?php $im = imagecreatefrompng ( php.png ); $rgb = imagecolorat ( $im , 10 , 15 ); $r = ( $rgb 16 ) 0xff ; $g = ( $rgb 8 ) 0xff ; $b = $rg
php 手册中有如下说明:
imagecolorat
(php 4, php 5)
imagecolorat — get the index of the color of a pixel
> 16 ) & 0xff ;
$g = ( $rgb >> 8 ) & 0xff ;
$b = $rgb & 0xff ;
var_dump ( $r , $g , $b );
?>
于是可以写一个专门的处理函数:
function getimagemaincolor($strurl) {
$imageinfo = getimagesize($strurl);
//图片类型
$imgtype = strtolower(substr(image_type_to_extension($imageinfo[2]), 1));
//对应函数
$imagefun = ‘imagecreatefrom’ . ($imgtype == ‘jpg’ ? ‘jpeg’ : $imgtype);
$i = $imagefun($strurl);
//循环色值
$rcolornum=$gcolornum=$bcolornum=$total=0;
for ($x=0;$x for ($y=0;$y $rgb = imagecolorat($i,$x,$y);
//三通道
$r = ($rgb >> 16) & 0xff;
$g = ($rgb >> & 0xff;
$b = $rgb & 0xff;
$rcolornum += $r;
$gcolornum += $g;
$bcolornum += $b;
$total++;
}
}
$rgb = array();
$rgb['r'] = round($rcolornum/$total);
$rgb['g'] = round($gcolornum/$total);
$rgb['b'] = round($bcolornum/$total);
return $rgb;
}
原文地址:php如何识别图片的主颜色, 感谢原作者分享。