Page 1 of 1
How can I find the number of channels in an image ?
Posted: Mon Jun 04, 2007 3:46 am
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 ?
Posted: Mon Jun 04, 2007 7:25 am
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.
Posted: Mon Jun 04, 2007 7:35 am
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
Posted: Mon Jun 04, 2007 7:56 am
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.

Posted: Mon Jun 04, 2007 8:07 am
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.
Posted: Mon Jun 04, 2007 8:14 am
by feyd
You could just analyze the headers. It's not as difficult as you may think.
Posted: Mon Jun 04, 2007 8:15 am
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?
Posted: Mon Jun 04, 2007 8:17 am
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 ..
Posted: Mon Jun 04, 2007 9:04 am
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.
Posted: Mon Jun 04, 2007 9:44 am
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.
GIFs don't use channels.
superdezign wrote:JPEGs and BMPs don't.
The BMP format supports an 8bit alpha channel.
Posted: Mon Jun 04, 2007 9:46 am
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.
Posted: Mon Jun 04, 2007 12:44 pm
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.
GIFs don't use channels.
superdezign wrote:JPEGs and BMPs don't.
The BMP format supports an 8bit alpha channel.
BMPs can have an alpha channel? Interesting.