function imgColor($imgUrl) { $imageInfo = getimagesize($imgUrl); $imgType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1)); $imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType); $i = $imageFun($imgUrl); $rColorNum = $gColorNum = $bColorNum = $total = 0; 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; $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; } $rgb = imgColor("图片地址"); print_r($rgb); //将打印出一个数组
根据函数我们可以理解到,此函数运行时会依次遍历所有的像素点。然后取出现最多的像素点。
改进一下函数:
function imgColor($imgUrl) { $imageInfo = getimagesize($imgUrl); $imgType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1)); $imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType); $i = $imageFun($imgUrl); $rColorNum = $gColorNum = $bColorNum = $total = 0; for ($x = 50; $x < imagesx($i) - 50; $x+=20) { for ($y = 50; $y < imagesy($i) - 50; $y+=20) { $rgb = imagecolorat($i, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 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; } $rgb = imgColor("图片地址"); print_r($rgb); //将打印出一个数组 /** * x=50 * imagesx($i) - 50 * y=50 * imagesy($i) - 50 * 相当于 margin 属性 **/
$gray = '255,255,255'; $grayLevel = $img['r'] * 0.299 + $img['g'] * 0.587 + $img['b'] * 0.114; if ($grayLevel >= 150) { $gray = '0,0,0'; }
希望能帮到需要的朋友。转载请注明出处。谢谢。
微信
支付宝