Created
May 5, 2012 03:32
-
-
Save jackilyn/2599386 to your computer and use it in GitHub Desktop.
Determine the dominant color of an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //This code will be super useful for people managing images or photography website. With it, you can analyze any image and get its dominant color (R, G, or B). | |
| $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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment