在 PHP 代码中,给定一个 .png 图像路径,我需要检测该图像的位深度。我怎样才能做到这一点?
我已尝试使用 getImageSize() 并读取 bits
,如下例代码所示,但对于 24 位/32 位图像,它始终返回“8”。
请帮忙。
class Utils {
//Ham de lay bits cua image
public static function getBits($image) {
$info = getImageSize($image);
return $info['bits'];
}
}
最佳答案
PNG 图像不支持 channel 和位 by getimagesize()
.但是,您可以使用一个小函数来获取这些值: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
)
它返回 channel 和位,就像某些人期望的那样来自 getimagesize()
旁边是一些特定于 PNG 格式的更多信息。位和 channel 旁边的值的含义是 documented in the PNG specification .
关于php - PHP 如何获取给定 PNG 图像文件的位深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6355692/