问题描述
|
在PHP代码中,给定.png图像路径,我需要检测该图像的位深。我怎样才能做到这一点?
我尝试使用getImageSize()并读取以下示例代码中的“ 0”,但对于24位/ 32位图像,它始终返回\'8 \'。
请帮忙。
class Utils {
//Ham de lay bits cua image
public static function getBits($image) {
$info = getImageSize($image);
return $info[\'bits\'];
}
}
解决方法
getimagesize()
不支持通道和位的PNG图像。但是,您可以使用一个小函数来获取以下值:get_png_imageinfo()
:
$file = \'Klee_-_Angelus_Novus.png\';
$info = get_png_imageinfo($file);
print_r($info);
给您提供示例图片:
Array
(
[bit-depth] => 4
[bits] => 4
[channels] => 1
[color] => 3
[color-type] => Indexed-colour
[compression] => 0
[filter] => 0
[height] => 185
[interface] => 0
[width] => 291
)
它返回通道和位,就像某些人希望从“ 2”中返回它们一样,再返回一些特定于PNG格式的信息。 PNG规范中记录了位和通道旁边的值的含义。
, 从getImageSize文档中:
bits是每种颜色的位数。
因此8位是正确的,因为如果有三个通道(RGB),每个通道有8位,则最终总共有24位。附加的alpha通道为您提供了另外8位,总计32。
尝试返回此:
return $info[\'channels\'] * $info[\'bits\'];
但是,这不适用于每种图像类型。阅读有关gif和jpeg如何工作的文档。