How can I find the number of channels in an image ?
Moderator: General Moderators
-
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 ?
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 ?
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 ?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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
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
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
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.
-
newbie_php_123
- Forum Newbie
- Posts: 4
- Joined: Mon Jun 04, 2007 3:41 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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:Hi,
I have tried what you have suggested with no luck.
Most of the functions you mentioned are SET functions, rather than GET functions ...
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 wrote:It's odd such a simple problem does not have a simple solution.
-
newbie_php_123
- Forum Newbie
- Posts: 4
- Joined: Mon Jun 04, 2007 3:41 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
GIF file might have one... imagecolortransparent().
JPEGs and BMPs don't.
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:Well, PNG files always have an alpha channel, but it's not always utilized.
GIFs don't use channels.superdezign wrote:GIF file might have one... imagecolortransparent().
The BMP format supports an 8bit alpha channel.superdezign wrote:JPEGs and BMPs don't.
The quick way: Make the person uploading the image define whether there's an alpha channel or not.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 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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
onion2k wrote: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:Well, PNG files always have an alpha channel, but it's not always utilized.
GIFs don't use channels.superdezign wrote:GIF file might have one... imagecolortransparent().
The BMP format supports an 8bit alpha channel.superdezign wrote:JPEGs and BMPs don't.
BMPs can have an alpha channel? Interesting.