How can PHP get a bit of depth for a given PNG image file?

In PHP code, given the path of the .png image, I need to determine the bit depth of this image. How can i do this?

I tried using getImageSize () and read bits as below code example, but it always returns β€œ8” for a 24-bit / 32-bit image.

Please, help.

 class Utils { //Ham de lay bits cua image public static function getBits($image) { $info = getImageSize($image); return $info['bits']; } } 
+4
source share
2 answers

PNG images are not supported for channels and getimagesize() bits. However, to get these values, you can use a small function: get_png_imageinfo() :

 $file = 'Klee_-_Angelus_Novus.png'; $info = get_png_imageinfo($file); print_r($info); 

Gives you an example image:

 Array ( [bit-depth] => 4 [bits] => 4 [channels] => 1 [color] => 3 [color-type] => Indexed-colour [compression] => 0 [filter] => 0 [height] => 185 [interface] => 0 [width] => 291 ) 

It returns channels and bits, and some of them would expect from getimagesize() along with additional information related to the PNG format. The meaning of the values ​​next to the bits and channels is documented in the PNG specification .

+4
source

From the getImageSize documentation :

bit is the number of bits for each color.

So, 8 bits are correct because if there are three channels (RGB) with eight bits each, you will get a total of 24 bits. The extra alpha channel gives you eight more bits, a total of 32.

Try returning this:

 return $info['channels'] * $info['bits']; 

However, this does not work for all types of images. Read the documentation on how gif and jpeg work.

+8
source

All Articles