Jan
08

PHP and Detecting Colors


Tag: drupal, php

For the the past year and a half, I've been working to build up a couple of sites, including BagIdiot.com and ShoeIdiot.com.  These sites are both twists on product comparison engines, one focused on bag (handbags, luggage etc) and the other on...  yeah, shoes.

I've written before about the pieces and parts I used to put them together, including drupal running under apache.  Recently I've added a new feature that shows the color(s) of the products and includes those colors in the search index.  This feature was put together by modifying some handy code I was able to find on the web, including:

Extracting the most common colors was easy enough and the php package above meant that I didn't have to code it.  All you do is open the image and look at it pixel by pixel to figure out the color.  The code I implemented from phpclasses.org made this trivial but then I ended up with a list of hex color code and how many times that color was used in the image.  Now I needed to turn those hex codes into English that is meaningful to users and that is where the code from github came in.  That code is a phpized version of a nice javascript that you can see here only it wasn't quite done.  I made a few changes to stick the hex color codes and associated names into a DB table.

So now I had all of the pieces that I needed, a class to pull out the colors and the code to take those colors and find the closest matching name.  I'm only using the top three colors and then tossing out any duplicates that arrise when the hex codes are converted to text and then displaying this on the product pages and stuffing it into the solr index.  This work is done whenever a product page is loaded for the first time and then simply pulled out of the database.

It works fairly well and it mostly returns the colors you would expect.  For example,

this image ended up being tagged with:  Cinnabar (Red) , Coral Red, Radical Red.   The DB of names has the hex color code, the color name and then a sort of hue or color family.  If that family is included in the name of the color, i.e. Coral Red, I don't bother including it in the color list and if it's not there, I do as in Cinnabar (Red).  You can see the 1,600 or so different colors.

The thousands of products are still being processed so the index is not finished yet, but it is working quite well.  It's nice that a search on something like "shiraz" returns exactly what I was hopping for it to return.

I'm sticking the colors, along with the image name and node ID into a table rather than using CCK fields attached to the product nodes which is probably the "right" way to do it, but it is working for me.  The work is being done in the template's page.tpl.php file if the page being loaded is a product node.  If a value is in the DB, it uses it, if not, it figures out the colors to use, stick them into the DB and uses them for the meta keywords and displaying to the user.  When indexing the content, solr just queries that table based upon the node id to get the text colors to use.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
7 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
The right of the citizens to bear arms in defense of themselves and the State shall not be questioned.
Constitution Of The Commonwealth Of Pennsylvania

A well regulated militia, being necessary to the security of a free state, the right of the people to keep and bear arms, shall not be infringed.
Second Amendment to The Constitution of the United States of America

RSS FeedFollow Greg on Twitter! | Privacy Policy | Terms and Conditions
 Bobby Rahal BMW Sucks | PR

 

Copyright © 2012 Greg Stoltz

Powered by Drupal, an open source content management system