Home  >  Article  >  Backend Development  >  Two ways to get the average color value of an image in PHP

Two ways to get the average color value of an image in PHP

PHP中文网
PHP中文网Original
2017-03-30 17:06:072025browse

Two methods for php to get the average color value of a picture:

$i=imagecreatefromjpeg("jbxue.jpg");
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>>4)&0xff;
$b=$rgb&0xff;
$rTotal+=$r;
$gToal+=$g;
$bToal+=$b;
$total++;
}
}

$rAverage=round($rTotal/$total);
$gAverage=round($gTotal/$total);
$bAverage=round($bTotal/$total);

Example 2, get the value of the color of the picture.

<?php
/*
*文件:image_get_point.php
*功能:获取图片指定某点的颜色值
*整理:bbs.it-home.org
*/
function dec2hex($dec)
{
return strtoupper($dec>15?dechex($dec):(&#39;0&#39;.dechex($dec)));
}
$im = imagecreatefrompng(&#39;http://localhost/image_arc.php&#39;);
$rgb = imagecolorat($im,20,20);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$RGB = dec2hex($r).dec2hex($g).dec2hex($b);
echo "dec:$r-$g-$b<br />hex:#$RGB";
?>

The above are the content of the two methods of obtaining the average color value of the image in PHP. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn