Here is a function wich check if a file is an image or not.
The file type is determined following the signing of the header.
//------------------------------------------------------------------------------------------------------
// AS3/AIR Image detection with unknow file type extention.
// V 1.0
//------------------------------------------------------------------------------------------------------
public function GetImageFileType(TestFile : File) : String {
var extention : String = "";
var Bytes : ByteArray = new ByteArray();
var BmpStream : FileStream = new FileStream();
Bytes.endian = Endian.LITTLE_ENDIAN;
BmpStream.open(TestFile, FileMode.READ);
BmpStream.position = 0;
BmpStream.readBytes(Bytes, 0, 8 );
BmpStream.close();
//-------------------------------------------------------
var signature : int = Bytes.readShort();
if (signature == 0x4D42) {
extention = "bmp";
} else {
Bytes.position = 0;
signature = Bytes.readInt()& 0xFFFFFF;
if (signature == 0xFFD8FF) {
extention = "jpg";
} else {
Bytes.position = 0;
signature = Bytes.readInt()& 0xFFFFFFFF;
if (signature == 0x38464947 ) {
extention = "gif";
} else {
Bytes.position = 0;
signature = Bytes.readInt();
if (signature == 0x474E5089 ) {
signature = Bytes.readInt();
if (signature == 0x0A1A0A0D ) {
extention = "png";
}
}
}
}
}
return extention;
}
