Get the dominant color of any image.


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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);

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.