• 0

PHP - Get Colour From Image


Question

OK, here's my thought:

PHP based site.

Loads image from an array of images to use as header

Get's colour information from the image

Chooses most common colour to use as a major highlight colour for site

And it's that last step I'm struggling with. I can get an array of x colours from the image using this script:

<?php
function colorPalette($imageFile, $numColors, $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(!$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'] / 0x33)) * 0x33);
         $green = round(round(($rgb['green'] / 0x33)) * 0x33);
         $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
         $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
         if(array_key_exists($thisRGB, $colors))
         {
            $colors[$thisRGB]++;
         }
         else
         {
            $colors[$thisRGB] = 1;
         }
      }
   }
   arsort($colors);
   return array_slice(array_keys($colors), 0, $numColors);
}
// sample usage:
$palette = colorPalette('rmnp8.jpg', 10, 4);
echo "<table>\n";
foreach($palette as $color)
{
   echo "<tr><td style='background-color:#$color;width:2em;'> </td><td>#$color</td></tr>\n";
}
echo "</table>\n"; 

But then choosing a colour that is suitable (eg, not a grey or near a grey, not near white or black)...

An suggestions?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

First off, to find the common colour you just need to do a foreach loop.

<?php

  function commonColour ( $_colors ) {

    $_commonColour = null;

    foreach ($_colors as $_RGB => $_count) {

      if ($_commonColour === null) {
        /* do nothing */
      } else if ($_colors[$_commonColour] >= $_count) {

        if ($_RGB !== /* colour code */ && $_RGB !== /* colour code */) {
          break;
        }

      }

      $_commonColour = $_RGB;

    }

    return $_commonColour;

  }

?>

This code here is key to filter out unwanted colours:

if ($_RGB !== /* colour code */ && $_RGB !== /* colour code */) {

There a number of different ways to filter out unwanted colours, it depends on what colours or colour range you don't want. In my example I filtered by specific colours, but you can check if all the hex values are the same, which will catch white, grey and black.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.