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
Release: 2016-07-25 08:43:01
Original
1106 people have browsed it

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


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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template