Home > Backend Development > PHP Tutorial > 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中文网
Release: 2023-02-28 20:12:02
Original
2090 people have browsed it

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);
Copy after login

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";
?>
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template