Finding the bit depth of an image

I have some images with different formats (jpg, webp, png, …). I’m coding a script (in php) to gather some infos from these images (like dimensions, resolution, bit-depth and …). Since with php built-in functions, just for jpg format (and maybe another format), it’s possible to obtain bit-depth property of an image; I tried Imagick functions to do so. I tried this:

        $image = new Imagick($tmpName);
        $colorspace = $image->getImageColorspace();
        $bitsPerChannel = $image->getImageDepth();
        $channels = 0;
        switch ($colorspace) {
            case Imagick::COLORSPACE_SRGB:
                $channels = 3;
                break;
            case Imagick::COLORSPACE_CMYK:
                $channels = 4;
                break;
            case Imagick::COLORSPACE_GRAY:
                $channels = 1;
                break;
        }
        
        if ($image->getImageAlphaChannel()) {
            $channels += 1; // add alpha
        }
        
        $totalBitsPerPixel = $bitsPerChannel * $channels;

But the value of $totalBitsPerPixel is different with “bit-depth” property which Windows shows in “Properties” of image.

I searched stackoverflow, googled all kind of functions, chatted with Sider(Chrome), but couldn’t find the reason of this. What I’m looking for is: Windows shows a bit-depth property for each image file. How to acquire that in php? Giving advice would be appreciated. I attached a sample image:

You’re on the right track using Imagick in PHP to analyze image metadata. However, the reason you’re getting different values for bit-depth than what Windows shows under “Properties” is due to differences in terminology and interpretation.

What Windows shows as “Bit Depth”:

Windows Explorer shows bit-depth per pixel, which includes all channels, e.g.:

Channel Setup Bits Per Channel Channels Windows Shows
RGB 8 3 24
RGBA 8 4 32
Grayscale 8 1 8
CMYK 8 4 32

But Imagick’s getImageDepth() typically returns bits per channel, not per pixel. You must multiply it by the number of channels, which you attempted correctly.

Key issues to address:

  1. getImageDepth() returns the Quantum Depth, not necessarily the file’s native bit-depth.
  • Quantum depth could be 8, 16, or 32 depending on how ImageMagick is compiled.
  • It may not reflect actual saved image file depth unless the image was read without conversion.
  1. You should use getImageChannelDepth() instead:
$depthRed = $image->getImageChannelDepth(Imagick::CHANNEL_RED);
$depthGreen = $image->getImageChannelDepth(Imagick::CHANNEL_GREEN);
$depthBlue = $image->getImageChannelDepth(Imagick::CHANNEL_BLUE);
$depthAlpha = $image->getImageChannelDepth(Imagick::CHANNEL_ALPHA); // if alpha exists

Then take the max or average depending on your purpose.

Accurate Total Bits Per Pixel

$image = new Imagick($tmpName);
$hasAlpha = $image->getImageAlphaChannel();

$colorSpace = $image->getImageColorspace();
$channels = match ($colorSpace) {
    Imagick::COLORSPACE_SRGB => 3,
    Imagick::COLORSPACE_CMYK => 4,
    Imagick::COLORSPACE_GRAY => 1,
    default => 3, // fallback
};

if ($hasAlpha) {
    $channels += 1;
}

$bitsPerChannel = $image->getImageChannelDepth(Imagick::CHANNEL_RED); // assume consistent

$totalBitsPerPixel = $bitsPerChannel * $channels;

echo "Bit Depth (per pixel): " . $totalBitsPerPixel;

This should now match what Windows shows (e.g., 24, 32, 8, etc.)

###Optional: Use identify CLI (if installed)

For 100% accurate info:

identify -format "%z bits-per-pixel" image.png

You can call this in PHP:

$output = shell_exec("identify -format \"%z\" " . escapeshellarg($file));

Final Notes:

  • If Imagick still reports incorrect depth (especially with PNG or WebP), it could be due to internal upscaling by ImageMagick.
  • You can also try using getimagesize() for basic format info, but not bit depth.