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:
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.