Posted By

o0110o on 10/26/12


Tagged

php color php-gd


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

o0110o


Get the dominant color of any image.


 / Published in: PHP
 

URL: http://forums.devnetwork.net/viewtopic.php?t=39594

Point the script to an image and get its dominant color.

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

Report this snippet  

You need to login to post a comment.