How to check a PNG for grayscale/alpha color type?

前端 未结 3 1319
旧时难觅i
旧时难觅i 2021-01-12 09:07

PHP and GD seem to have trouble creating images from PNGs of type greyscale with alpha when using imagecreatefrompng(). The results are incredibly distorted.

3条回答
  •  -上瘾入骨i
    2021-01-12 09:45

    The colour type of a PNG image is stored at byte offset 25 in the file (counting from 0). So if you can get hold of the actual bytes of the PNG file, simply look at byte 25 (I don't do PHP, so I don't know how to do that):

    • 0 - greyscale
    • 2 - RGB
    • 3 - RGB with palette
    • 4 - greyscale + alpha
    • 6 - RGB + alpha

    The preceding byte (offset 24) gives the number of bits per channel. See the PNG spec for more details.

    In a slight twist a PNG file may have "1-bit alpha" (like GIFs) by having a tRNS chunk (when it is colour type 0 2 or 3).

提交回复
热议问题