php - PHP 如何获取给定 PNG 图像文件的位深度?

标签 php png bit-depth

在 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/

相关文章:

css - 透明PNG/GIF雕刻出背后的背景图

ImageMagick,用不透明白色替换半透明白色

php - 为什么中间件不实现接口(interface)?

php正则表达式过滤数据

javascript - 在同一页面上的 php 中获取 JavaScript 变量

android - 在 Android 中显示巨大的图像

macos - 从命令行减少PNG文件的位深度

python - 确定 wav 文件的位深度

php - WordPress SSL 通过 Cloudflare 重定向