claviska / SimpleImage

A PHP class that makes working with images and GD as simple as possible.
MIT License
1.38k stars 383 forks source link

Method: Most used color #144

Closed lorvent closed 8 years ago

lorvent commented 8 years ago

Hello, Great library, want to use for some batch operations on cli.

do you have any plans to add to find which color is most used in an image?

Thanks.

claviska commented 8 years ago

We talked about this in #27. Two problems that still exist are licensing and overhead.

It's easy enough to use color-thief-php on its own, so doubling the size of SimpleImage just to support one feature and having to worry about licensing conflicts isn't really worth it at this point.

lorvent commented 8 years ago

why we need to use that package code?

its a simple code where we have to find color of each pixel then find out which is used most,

there are many examples exists in SO aswell.

infact i never know about that package too.

claviska commented 8 years ago
  1. The license is incompatible.
  2. Finding the dominant color involves more than just counting the most frequently used color values. That's easy to do, but it's not useful in a real world application. There are flaws with that method, for example, antialiasing will cause pixels to be visually similar but not the same color value. A proper solution will normalize visually similar colors, which is especially important for photographs and JPEG images.

The library I linked to is a fork of color-thief. You can see how well the dominant color and palettes are generated in the demo. It uses quantize.js to achieve spot on results.

To use color-thief-php, simply require it:

composer require ksubileau/color-thief-php

And grab the dominant color like this:

$dominantColor = \ColorThief\ColorThief::getColor($image);

To get a full color palette, do this instead:

$palette = \ColorThief\ColorThief::getPalette($image, 8);