Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

image

tiff

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 particularly interested in TIFF files) but what sort of procedure would be useful to determine what kind of image format a file is without having to read through the entire file?

BONUS: What if the image is a Base64-encoded string? Any reliable way to infer it before decoding it?

like image 633
Tom Kidd Avatar asked Sep 09 '08 19:09

Tom Kidd


Video Answer


2 Answers

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'.

like image 123
LarryF Avatar answered Sep 19 '22 12:09

LarryF


Most image file formats have unique bytes at the start. The unix file command looks at the start of the file to see what type of data it contains. See the Wikipedia article on Magic numbers in files and magicdb.org.

like image 28
Greg Hewgill Avatar answered Sep 21 '22 12:09

Greg Hewgill