简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM