Connecting Tech Pros Worldwide Help | Site Map

determine if image is grayscale

tjh
Guest
 
Posts: n/a
#1: Jul 17 '05

hi -

i'm a bit new to PHP - i've been messing around with the GD2 image functions
and was wondering:

i see a lot of information about converting a color image to grayscale but i
can't figure out how to determine whether an image (GIF or JPG) is grayscale
in the first place.

is there a way to do this in PHP?

thanks for any advice!

tjh


Andy Hassall
Guest
 
Posts: n/a
#2: Jul 17 '05

re: determine if image is grayscale


On Thu, 09 Sep 2004 17:24:43 GMT, "tjh" <t.hickey@ten.tsacmoc> wrote:
[color=blue]
>i'm a bit new to PHP - i've been messing around with the GD2 image functions
>and was wondering:
>
>i see a lot of information about converting a color image to grayscale but i
>can't figure out how to determine whether an image (GIF or JPG) is grayscale
>in the first place.
>
>is there a way to do this in PHP?[/color]

If it's a paletted image, imagecolorsforindex(), or for a true colour image
loop through the pixels and usage imagecolorat().

All the colours should have their red, green and blue components equal, or
near equal, if it's a greyscale image.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Pjotr Wedersteers
Guest
 
Posts: n/a
#3: Jul 17 '05

re: determine if image is grayscale


Andy Hassall wrote:[color=blue]
> On Thu, 09 Sep 2004 17:24:43 GMT, "tjh" <t.hickey@ten.tsacmoc> wrote:
>[color=green]
>> i'm a bit new to PHP - i've been messing around with the GD2 image
>> functions and was wondering:
>>
>> i see a lot of information about converting a color image to
>> grayscale but i can't figure out how to determine whether an image
>> (GIF or JPG) is grayscale in the first place.
>>
>> is there a way to do this in PHP?[/color]
>
> If it's a paletted image, imagecolorsforindex(), or for a true colour
> image loop through the pixels and usage imagecolorat().
>
> All the colours should have their red, green and blue components
> equal, or near equal, if it's a greyscale image.[/color]

Looping throught all pixels may become tediously sllloooooowwww for bigger
pix. Why bother, just grayscale them regardless of previous state. Is that
an option for you ?
Pjotr


Chung Leong
Guest
 
Posts: n/a
#4: Jul 17 '05

re: determine if image is grayscale


"tjh" <t.hickey@ten.tsacmoc> wrote in message
news:vl00d.163154$mD.122719@attbi_s02...[color=blue]
>
> hi -
>
> i'm a bit new to PHP - i've been messing around with the GD2 image[/color]
functions[color=blue]
> and was wondering:
>
> i see a lot of information about converting a color image to grayscale but[/color]
i[color=blue]
> can't figure out how to determine whether an image (GIF or JPG) is[/color]
grayscale[color=blue]
> in the first place.
>
> is there a way to do this in PHP?
>
> thanks for any advice!
>
> tjh
>[/color]

For JPEG, you can call getimagesize() to get the number of color channels.
If it's one, then the image is greyscale. For GIF, you can scan the palette.


Closed Thread