Is there a way to infer what image format a file is, without reading the entire file?

前端 未结 4 1071
情歌与酒
情歌与酒 2021-02-20 00:19

Is there a good way to see what format an image is, without having to read the entire file into memory?

Obviously this would vary from format to format (I\'m particular

4条回答
  •  再見小時候
    2021-02-20 00:31

    Sure there is. Like the others have mentioned, most images start with some sort of 'Magic', which will always translate to some sort of Base64 data. The following are a couple examples:

    A Bitmap will start with Qk3

    A Jpeg will start with /9j/

    A GIF will start with R0l (That's a zero as the second char).

    And so on. It's not hard to take the different image types and figure out what they encode to. Just be careful, as some have more than one piece of magic, so you need to account for them in your B64 'translation code'.

提交回复
热议问题