Home > Article > Backend Development > Two ways to get the average color value of an image in PHP
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):('0'.dechex($dec))); } $im = imagecreatefrompng('http://localhost/image_arc.php'); $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)!