Home >Backend Development >PHP Tutorial >PHP code snippet to determine the main color of a picture

PHP code snippet to determine the main color of a picture

WBOY
WBOYOriginal
2016-07-25 08:43:011092browse

This code can help you determine the main color of any picture, using a simple statistical algorithm to achieve

  1. $i = imagecreatefromjpeg("image.jpg");
  2. for ($x=0;$x< imagesx($i);$x++) {
  3. for ($y=0;$y $rgb = imagecolorat($i,$x,$y);
  4. $r = ($rgb >> 16) & 0xFF;
  5. $g = ($rgb >> & 0xFF;
  6. $b = $rgb & 0xFF;
  7. $rTotal += $r;
  8. $gTotal += $g;
  9. $bTotal += $b;
  10. $total++;
  11. }
  12. }
  13. $rAverage = round($rTotal/$total);
  14. $gAverage = round($gTotal/$total);
  15. $bAverage = round($bTotal/$total);
Copy code

Main color, PHP


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