How can I find the number of channels in an image ?

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
newbie_php_123
Forum Newbie
Posts: 4
Joined: Mon Jun 04, 2007 3:41 am

How can I find the number of channels in an image ?

Post by newbie_php_123 »

Hi all,

This is my first message here.

I would like to know how many channels an image has.

The image may be a BMP, JPEG, PNG, or any number of other formats (but these are the most common).
The image might also have an ALPHA channel.
The image might be compressed (like in PNG - you can decide how compressed you want the image to be).

At first I used the "getimagesize" function to retreive the number of channels,
but I realised :
1. It some times returns 0.
2. In images with an ALPHA channel - it doesn't count that channel.

Does anyone here have a solution for me please ?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm not quite sure what you're trying to accomplish. Different types of images work very differently and should be handled as such. Maybe get_headers() could help.

And maybe you should read up on more of the functions and different ways to handle different files with the GD library in PHP.
newbie_php_123
Forum Newbie
Posts: 4
Joined: Mon Jun 04, 2007 3:41 am

Post by newbie_php_123 »

All I want to do is know if the image has an ALPHA channel or not ...
Can anyone please please help me accomplish that ?
I've read dozens of threads about this already.
getimagesize seems to return 3 channels even when there is a 4th ALPHA channel present ...
And I've also gone over the GD library and could not find a cure there ...
please help someone
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

imagecolortransparent() can give you the color that is automatically given an alpha of 0 (mostly exists for gif images).

imagesavealpha() attempts to set a flag to save alpha information in an image... It may fail on images without the capability.

imagecolorclosestalpha() takes an RGB value and an alpha value, and gives you the index of a color that's almost like it. I assume that using an alpha of 0 would give you the closest color to being an alpha of 0 (unless RGB takes precedence... you should try it out.) I think that, in combination with imagecolorsforindex() should give you the closest color's alpha value. If it doesn't exist, it may be safe to assume that the image has no alpha values.

All of this is from the GD library... I know the fact that it has a lot of functions is intimidating, but the documentation is there so that when something needs to be done, you've got a place to find the tools in order to do it. It's no fun to read it, but you're bound to need it. :wink:
newbie_php_123
Forum Newbie
Posts: 4
Joined: Mon Jun 04, 2007 3:41 am

Post by newbie_php_123 »

Hi,

I have tried what you have suggested with no luck.
Most of the functions you mentioned are SET functions, rather than GET functions ...

Any other solution ?

It's odd such a simple problem does not have a simple solution.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You could just analyze the headers. It's not as difficult as you may think.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

newbie_php_123 wrote:Hi,

I have tried what you have suggested with no luck.
Most of the functions you mentioned are SET functions, rather than GET functions ...
The majority of the "set" functions in the GD library work as "get" functions depending on how many arguments you give them. Did you read their documentations?
newbie_php_123 wrote:It's odd such a simple problem does not have a simple solution.
Well, it's an odd problem to have. Is there a reason you need to know if an image has an alpha channel?
newbie_php_123
Forum Newbie
Posts: 4
Joined: Mon Jun 04, 2007 3:41 am

Post by newbie_php_123 »

Yes,
I need to organize the images the user has uploaded to my site as images WITH and images WITHOUT an ALPHA channel ..
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, PNG files always have an alpha channel, but it's not always utilized.

GIF file might have one... imagecolortransparent().

JPEGs and BMPs don't.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

superdezign wrote:Well, PNG files always have an alpha channel, but it's not always utilized.
Not true. PNGs can have 1 channel (greyscale), 2 channels (greyscale + alpha), 3 channels (RGB), or 4 channels (RGBA). Or they can be indexed which is technically 0 channels because the information is stored using a mapped palette rather than channels.
superdezign wrote:GIF file might have one... imagecolortransparent().
GIFs don't use channels.
superdezign wrote:JPEGs and BMPs don't.
The BMP format supports an 8bit alpha channel.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

newbie_php_123 wrote:I need to organize the images the user has uploaded to my site as images WITH and images WITHOUT an ALPHA channel ..
The quick way: Make the person uploading the image define whether there's an alpha channel or not.

The slow way: Loop through all the pixels of the uploaded image checking each one to see if it has an alpha value using imagecolorat(). It'll be painfully slow on big images.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

onion2k wrote:
superdezign wrote:Well, PNG files always have an alpha channel, but it's not always utilized.
Not true. PNGs can have 1 channel (greyscale), 2 channels (greyscale + alpha), 3 channels (RGB), or 4 channels (RGBA). Or they can be indexed which is technically 0 channels because the information is stored using a mapped palette rather than channels.
superdezign wrote:GIF file might have one... imagecolortransparent().
GIFs don't use channels.
superdezign wrote:JPEGs and BMPs don't.
The BMP format supports an 8bit alpha channel.

:oops:

BMPs can have an alpha channel? Interesting.
Post Reply