Skip to content

Instantly share code, notes, and snippets.

@mikesprague
Created July 26, 2013 16:15
Show Gist options
  • Save mikesprague/6090165 to your computer and use it in GitHub Desktop.
Save mikesprague/6090165 to your computer and use it in GitHub Desktop.
PHP: Get dominant color from an image (use final values get RGB value of dominant color, taken from: http://snipplr.com/view/67946/get-the-dominant-color-of-any-image/)
<?php
$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