How to tell if image is PNG24 or PNG8 and the number of bits and channels?

1.8k views Asked by At

I am trying to distinguish PNG-8 and PNG-24 images with getimagesize or Imagick, but I don't quite know how to to it.

getimagesize does not return channels for my PNGs and displays the mimetype instead. It works well for other images and shows the correct values, but for PNG it just shows nothing.

edit: Imagick is not installed in my environment but gdlib is...

Can anyone help me a bit?

Greetings,
Tom

edit2: Is it possible to do it like this?

    //create png for tests
    $testPng = imagecreatefrompng( $file );
    //test how many colors are used
    $meta .= 'colors: ' . imagecolorstotal( $testPng );
    $meta .= ' truecolor: ' . imageistruecolor( $testPng );
    //destroy the test image
    imagedestroy( $testPng );

And if truecolor is false or not set, it is a png24?

2

There are 2 answers

6
Pekka On

getimagesize() seems to do the trick:

bits is the number of bits for each color.

doesn't even need GD.

1
bcosca On