检测image类型及尺寸的erlang模块
Socklabs在其项目中需要限制用户上传的图片类型为gif,png和jpeg, 同时图片文件大小必须 < 4M, 图片尺寸小于2048*2048.这个模块很简单,以binary的方式读取文件,随后通过图片相应的格式解析,获取格式,尺寸相关信息.随后可以做出判断.
代码 下载:
-module(ipwfiles_image).
-export([image_type/1]).
-compile([export_all]).
image_type(File) when is_list(File) ->
case file:read_file(File) of
{ok, Data} ->
image_type(Data);
_ ->
{error, openfile}
end;
%% Gif header, width and height
%% http://www.etsimo.uniovi.es/gifanim/gif87a.txt
image_type(<<$G, $I, $F, $8, $9, $a, Width:16/little, Height:16/little, _/binary>>) ->
{gif, Width, Height};
image_type(<<$G, $I, $F, $8, $7, $a, Width:16/little, Height:16/little, _/binary>>) ->
[...]