Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check an image's format in Elixir

So let's assume I have a file stored somewhere and it can be either a jpeg file or a png file, what is the way to check what it is?

Should I simply check the extension? Or should I read the file's binary signature? Or how do I do it?

like image 496
NoDisplayName Avatar asked Nov 09 '15 05:11

NoDisplayName


2 Answers

Update: 4/2/2016 to include .JPG validation.

There's a fairly popular hex package Arc that might work for you. Extracted from Arc:

def validate({file, _}) do
  ~w(.jpg .jpeg .gif .png .JPG) |> Enum.member?(Path.extname(file.file_name))
end
like image 139
Donovan Dikaio Avatar answered Nov 09 '22 07:11

Donovan Dikaio


I guess you could extract the Magic number of the file and analyse it. That's fairly simple once you have the format specs

like image 1
Hécate Avatar answered Nov 09 '22 08:11

Hécate