Detecting white image background with PHP?

Is it possible with PHP (alternatively JS or something similiar) to detect if an image has a mostly white background?

It is because where I display my images there's a white background. And some images have a dark or colorful background, but some are white or mostly white. And I have a css class called img-shadow that adds a shadow to an image. So I would like to add that only if the background of an image is white or mostly white.

Here's an example to show you what I mean:

enter image description here

Even there are a lot of pitfalls in whole process, I would choose doing it using PHP.

Firstly, answer yourself following:

  • What exactly will be mostly white background? Does RGB(250,250,250) still counts?
  • Does PNG with alpha channel counts (RGBA)?
  • Does image with light/bright yellow counts?
  • Does image with black border (frame) and white center will stil counts as white background?
  • Will user be able to upload image?

You have to:

  1. Load image
  2. get histogram
  3. Analyze according to your rules
  4. Add meta info about particular image and generate css class on output.

On top of that I suggest:

  • Some sort of result caching or metadata storing so on every script execution images won't be analyzed
  • Check if image has border (develop algorythm by looking on sides of picture)
  • Treat alpha channel same as white color (maybe dark logo with round corners - no white color)