Analyse Image Colors Using PHP

December 4th, 2011 | Posted by ChrisG in Code Snippets - (Comments Off)

Analyse Image Colors Using PHP
PHP code to analyse colors in an image against color swatchs and then showing the most common colors used in the image. This code can be used as the basis for a search images by color function.

The basic program uses the PHP GD library to process the images and follows these general steps.

  1. Define the colors in the color swatches
  2. Create an image object using the image that is to be analysed, resizing the image if it is too large
  3. Create an image object and load the colors from the the color swatches into image palette. This is the palette that a GIF image uses. This is the comparison palette that the colors in the image being analysed will be compared to.
  4. Loop through all the pixels in the image and tally it against the closest match to the pixel in the comparison palette.
  5. Sort the tally results in descending order
  6. Display the top results

Click here to view full blog