Skip to content

Instantly share code, notes, and snippets.

@keiron77
Last active August 29, 2015 13:57
Show Gist options
  • Save keiron77/9769577 to your computer and use it in GitHub Desktop.
Save keiron77/9769577 to your computer and use it in GitHub Desktop.
This script will process an image, take the colour used at each pixel and list them in order of use
function colorPalette($imageFile, $granularity = 5){
$granularity = max(1, abs((int)$granularity));
$colors = array();
$size = @getimagesize($imageFile);
if($size === false){
user_error("Unable to get image size data");
return false;
}
//$img = @imagecreatefromjpeg($imageFile);
if ($size[2]==1)
$img = @imagecreatefromgif($imageFile);
if ($size[2]==2)
$img = @imagecreatefromjpeg($imageFile);
if ($size[2]==3)
$img = @imagecreatefrompng($imageFile);
if(!$img)
{
user_error("Unable to open image file");
return false;
}
for($x = 0; $x < $size[0]; $x += $granularity)
{
for($y = 0; $y < $size[1]; $y += $granularity)
{
$thisColor = imagecolorat($img, $x, $y);
$rgb = imagecolorsforindex($img, $thisColor);
$red = round(round(($rgb['red'] / 0×33)) * 0×33);
$green = round(round(($rgb['green'] / 0×33)) * 0×33);
$blue = round(round(($rgb['blue'] / 0×33)) * 0×33);
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
if(array_key_exists($thisRGB, $colors))
{
$colors[$thisRGB]++;
}
else
{
$colors[$thisRGB] = 1;
}
}
}
arsort($colors);
return $colors;
}
function getHtml2Rgb($str_color)
{
if ($str_color[0] == '#')
$str_color = substr($str_color, 1);
if (strlen($str_color) == 6)
list($r, $g, $b) = array($str_color[0].$str_color[1],
$str_color[2].$str_color[3],
$str_color[4].$str_color[5]);
elseif (strlen($str_color) == 3)
list($r, $g, $b) = array($str_color[0].$str_color[0], $str_color[1].$str_color[1], $str_color[2].$str_color[2]);
else
return false;
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
$arr_rgb = '('.$r.','. $g.','. $b.')';
// Return colors format liek R(255) G(255) B(255)
return $arr_rgb;
}
function totalpx($imageFile, $granularity)
{
$size = @getimagesize($imageFile);
$totpx = $size[0]*$size[1];
$pcrpx = round($totpx / $granularity);
return $pcrpx;
}
// sample usage:
$images = 'http://media.gizmodo.co.uk/wp-content/uploads/2012/08/virgin-trains-franchise.jpg';
$granularity = 4;
$palette = colorPalette($images, $granularity);
$total_pixel = totalpx($images, $granularity);
$colors_to_show = 255;
echo '<img src="'.$images.'"><br/>';
echo '<table border="1"><tr><td>Color</td><td>Color Hex</td><td>Color RGB</td><td>Count</td><td>Percentage</td></tr>';
for($h=0;$h<$colors_to_show;$h++) {
$color = array_keys($palette);
$hex = '#'.$color[$h];
$color_pixel = $palette[$color[$h]];
$percentage = ($color_pixel / $total_pixel) * 100;
echo '<tr><td style="background-color:'.$hex.';width:2em;"> </td><td>'.$hex.'</td><td>rgb'.getHtml2Rgb($hex).'</td><td>'.$palette[$color[$h]].'</td><td>'.number_format($percentage, 1).' %</td>';
}
echo '</table>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment