PHP 图片混合取色
一直想把网站主题搞成跟背景一个色调,所以就用到了PHP取色 .
原理也很简单利用For循环取整张图片的颜色RGB平均值,网上都有现成的 .
我只是整理出来, 有需要的童鞋可以来看看 .
我就直接贴我的代码了 .
<?php function getRgbimg($dec) { return strtoupper($dec>15?dechex($dec):('0'.dechex($dec))); } $im = imagecreatefromjpeg('http://image9.360doc.com/DownloadImg/2010/05/0911/3143484_2.jpg'); for ($x=0;$x<imagesx($im);$x++) { for ($y=0;$y<imagesy($im);$y++) { $rgb = imagecolorat($im,$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); $RGB = getRgbimg($rAverage).getRgbimg($gAverage).getRgbimg($bAverage); echo "dec:$rAverage-$gAverage-$bAverage<br />hex:#$RGB"; echo '<div style="background-color:'.$RGB.'; width:80px; height:80px"></div>'; ?>
CM部落 》原创,转载请保留文章出处。
本文链接:PHP 图片混合取色 https://www.itkz.cn/note/php-get-pic-color.html
版权声明:若无特殊注明,本文皆为《
正文到此结束
失心博客
可否和博主互换友链,谢谢!如果博主同意的话请发送邮件至:admin@3sx1.com,谢谢!
匿名