Skip to content
Advertisement

PHP how to detect if image contains color?

I am writing an image search engine, which allows users to search by colour. As part of this, I need to determine whether the image is grayscale (contains only black, white or shades of grey) or contains any color.

How can I detect if the image contains any pixels outside of black, white and shades of grey?

Advertisement

Answer

You’d have to walk through every pixel using imagecolorat() (see example #2).

Grayscale colours will have the same value for Red, Green, and Blue. If you find a pixel where the values differ, it’s an image that contains a colour (at least technically – with a colour like RGB(100,102,103) it will look grey to the human eye.).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement