Even though it's nice that you do the file parsing to see what the actual file format is, maybe it's enough to just look at the file ending? (i.e. if it ends with .png it's a png file)