在开发图片压缩服务的过程中遇到了一个问题:当图片服务器上的文件的扩展名不规范时(例如有些jpeg 文件的扩展名为.gif、有些文件的扩展名为".img",".10" 等等),图片压缩的程序从图片服务器请求这些文件,那么response 中的Content-Type 就存在问题,往往Content-Type 的内容与图片并不一致,甚至得到竟然是text/plain 之类的结果,从而导致无法选择恰当的编解码器对图片进行处理。
最后在 google 中搜到了 FILExt ,他的资料库中存储了上万种文件扩展名的相关信息,如对应的MimeType 、File Classification 等等,最重要的是里面有可以确定一个文件的文件类型的特征码(Identifier Characters),即文件的前n个字节,用这n个字节可以准确的辨认出文件的类型。
FILExt - The File Extension Source (http://filext.com/index.php)
FILExt is a detailed database of file extensions and programs that use them.
截至到2004-07-15,FILExt 中已经有了16828 条记录;你甚至可以通过FILExt 的blog 向FILExt 的库中添加你所掌握的某种新型文件的相关信息。
下面是在FileExt 查询gif 后得到结果
Extension: GIF
Program and/or Extension Function
Graphic Interchange Format
Graphic files in .GIF format can be manipulated by a large number of programs. Some programs, e.g., Fractint, us the GIF header to store program information so that when the file is opened the program sets itself to the state it was in when the file was last edited.
image/gif
image/x-xbitmap
image/gi_
XnView IrfanView GIF Format Fractint The GIMP
Hex: 47 49 46 38
ASCII: GIF8
or for GIF87a...
Hex: 47 49 46 38 37 61
ASCII: GIF87a
or for GIF89a...
Hex: 47 49 46 38 39 61
ASCII: GIF89a
(None or Unknown)
Various programs use this extension; too many to list individually. Take clues from the location of the file as a possible pointer to exactly which program is producing the file. The file's date and time can also help if you know which programs you were running when the file was written.
This is record 5048 last modified on 2004-02-02.
*/ --下面是从FILExt 中查到的另外几种常见图片的文件(bmp、jpeg、png、tiff)标识信息。
----------------------------
image/gif
image/x-xbitmap
image/gi_
Hex: 47 49 46 38
ASCII: GIF8
or for GIF87a...
Hex: 47 49 46 38 37 61
ASCII: GIF87a
or for GIF89a...
............................
Hex: 47 49 46 38 39 61
ASCII: GIF89a
----------------------------
----------------------------
image/jpeg
image/jpg
image/jp_
application/jpg
application/x-jpg
image/pjpeg
image/pipeg
image/vnd.swiftview-jpeg
image/x-xbitmap
............................
Hex: FF D8 FF
ASCII:
----------------------------
----------------------------
image/bmp
image/x-bmp
image/x-bitmap
image/x-xbitmap
image/x-win-bitmap
image/x-windows-bmp
image/ms-bmp
image/x-ms-bmp
application/bmp
application/x-bmp
application/x-win-bitmap
application/preview
............................
Hex: 42 4D
ASCII: BM
----------------------------
----------------------------
image/png
application/png
application/x-png
............................
Hex: 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
ASCII: .PNG........IHDR
----------------------------
----------------------------
image/tif
image/x-tif
image/tiff
image/x-tiff
application/tif
application/x-tif
application/tiff
application/x-tiff
............................
Hex: 49 49 2A 00
ASCII:
----------------------------