Home >Backend Development >PHP Tutorial >PHP detects the main color of the picture

PHP detects the main color of the picture

*文
*文Original
2017-12-27 11:24:251657browse

How to detect the main color of an image in php? This article mainly introduces the method of detecting the main color of the picture in PHP, and involves PHP's related operation skills for pictures. I hope to be helpful.

The example in this article describes the method of detecting the main color of the image in PHP. Share it with everyone for your reference. The specific implementation method is as follows:


$i = imagecreatefromjpeg("image.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 >> & 0xFF;
    $b  = $rgb & 0xFF;
    $rTotal += $r;
    $gTotal += $g;
    $bTotal += $b;
    $total++;
  }
}
$rAverage = round($rTotal/$total);
$gAverage = round($gTotal/$total);
$bAverage = round($bTotal/$total);

Related recommendations:

PHP Basic Tutorial Sixteenth Use of GD Library for graphics drawing

php image operation class, supports generating thumbnails, adding watermarks, and uploading thumbnails

Strong PHP image processing class

The above is the detailed content of PHP detects the main color of the picture. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:php crack anti hotlinkNext article:php crack anti hotlink