Encounter a requirement: Assume that there is a picture file, the true type of jpg, and someone lazy to copy a JPG directly, save as a PNG file with the same name, so that the AS3 read the file will not encounter problems, but the mobile phone C + + in reading files are encountered problems-!
Now you need to write a program to traverse all the files in the folder to find the file format "not normal" file. Our resources are mainly GIF, PNG, JPG, the beginning, I found an article on the Internet: according to the binary stream and file header get file type Mime-type, and then read the file binary header information, get its true file type, and the file type obtained by the suffix name comparison.
Later, when searching for information related to node image, find this article: Node.js module ranking>> (IMAGES)
And then filter to a module "Node-imageinfo", wrote an example to test (intentionally the JPG file directly modify the suffix named png):
function readUInt32 (buffer, offset, Bigendian) {
if (Buffer.readuint32) {
return Buffer.readuint32 (offset, Bigendian);
}
var value;
if (Bigendian) {
if (buffer.readuint32be) {
return Buffer.readuint32be (offset);
}
value = (Buffer[offset] <<) + (Buffer[offset+1] <<) + (b UFFER[OFFSET+2] << 8) + buffer[offset+3];
}
Else {
if (buffer.readuint32le) {
return Buffer.readuint32le (offset);
}
value = Buffer[offset] + (buffer[offset+1] << 8) + (buffer[offset+ 2] <<) + (Buffer[offset+3] << 24);
}
return value;
}
Function readUInt16 (buffer, offset, Bigendian) {
if (buffer.readuint16) {
& nbsp; return buffer.readuint16 (offset, Bigendian);
}
var value;
if (Bigendian) {
if (buffer.readuint16be) {
return buffer.readuint16be (offset);
}
value = (Buffer[offset] << 8) + buffer[offset+1];
}
Else {
if (buffer.readuint16le) {
return Buffer.readuint16le (offset);
}
value = Buffer[offset] + (buffer[offset+1] << 8);
}
return value;
}
function readbit (buffer, offset, bitoffset) {
if (Bitoffset > 7) {
Offset + + Math.floor (BITOFFSET/8);
Bitoffset = bitoffset% 8;
}
var b = Buffer[offset];
if (Bitoffset < 7) {
b >>>= (7-bitoffset);
}
var val = b & 0x01;
return Val;
}
Function readbits (buffer, offset, bitoffset, Bitlen, signed) {
var val = 0;
&nbs P
var neg = false;
if (signed) {
if readbit (buffer, offset, Bitoffset) > 0) {
neg = true;
}
bitlen--;
bitoffset++;
}
var bytes = [];
for (var i = 0; i < Bitlen; i++) {
var b = readbit (buffer, offset, bitoffset + i);
if (i>0 && (bitlen-i)% 8 = 0) {
Bytes.push (Val);
val = 0;
}
Val <<= 1;
Val |= b;
}
Bytes.push (Val);
val = new Buffer (bytes);
Val.negative = Neg?true:false;
return Val;
}
function Imageinfopng (buffer) {
var imageheader = [0x49, 0x48, 0x44, 0x52],
pos = 12;
if (!checksig (buffer, POS, Imageheader)) {
return false;
}
POS + 4;
return {
Type: ' Image ',
Format: ' PNG ',
MimeType: ' Image/png ',
Width:readuint32 (buffer, POS, true),
Height:readuint32 (buffer, pos+4, true),
};
}
function imageinfojpg (buffer) {
var pos = 2,
Len = Buffer.length,
Sizesig = [0xFF, [0xc0, 0xc2]];
while (Pos < len) {
if (Checksig (buffer, POS, Sizesig)) {
POS + 5;
return {
Type: ' Image ',
Format: ' JPG ',
MimeType: ' Image/jpeg ',
Width:readuint16 (buffer, pos+2, true),
Height:readuint16 (buffer, POS, true),
};
}
POS + 2;
var size = readUInt16 (buffer, POS, true);
pos + = size;
}
}
function imageinfogif (buffer) {
var pos = 6;
return {
Type: ' Image ',
Format: ' GIF ',
MimeType: ' Image/gif ',
Width:readuint16 (buffer, POS, false),
Height:readuint16 (buffer, pos+2, false),
};
}
function imageinfoswf (buffer) {
var pos = 8,
Bitpos = 0,
Val
if (buffer[0] = = 0x43) {
try {
If you have zlib available (NPM install zlib) then we can read compressed Flash files
Buffer = require (' zlib '). Inflate (Buffer.slice (8, 100));
pos = 0;
}
catch (ex) {
Can ' t get width/height of compressed Flash files ... yet (need zlib)
return {
Type: ' Flash ',
Format: ' SWF ',
MimeType: ' Application/x-shockwave-flash ',
Width:null,
Height:null,
}
}
}
var numbits = readbits (buffer, POS, Bitpos, 5) [0];
Bitpos + 5;
val = readbits (buffer, POS, Bitpos, Numbits, true);
var xmin = (Numbits > 9 readUInt16 (val, 0, true): Val[0]) * (val.negative? -1:1);
Bitpos + = Numbits;
val = readbits (buffer, POS, Bitpos, Numbits, true);
var xmax = (Numbits > 9 readUInt16 (val, 0, true): Val[0]) * (val.negative? -1:1);
Bitpos + = Numbits;
val = readbits (buffer, POS, Bitpos, Numbits, true);
var ymin = (Numbits > 9 readUInt16 (val, 0, true): Val[0]) * (val.negative? -1:1);
Bitpos + = Numbits;
val = readbits (buffer, POS, Bitpos, Numbits, true);
var ymax = (Numbits > 9 readUInt16 (val, 0, true): Val[0]) * (val.negative? -1:1);
return {
Type: ' Flash ',
Format: ' SWF ',
MimeType: ' Application/x-shockwave-flash ',
Width:Math.ceil ((xmax-xmin)/20),
Height:Math.ceil ((ymax-ymin)/20),
};
}
Function checksig (buffer, offset, sig) {
var len = sig.length
for (var i = 0; i < Len; i++) {
var b = buffer[i+offset],
s = sig[i],
m = false;
if (' number ' = typeof s) {
m = s = = b;
}
else {
for (var k in s) {
var o = s[k];
if (o = = b) {
m = true;
}
}
}
if (!m) {
return false;
}
}
return true;
}
Module.exports = function imageinfo (buffer, path) {
var pngsig = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
var jpgsig = [0xFF, 0xd8, 0xFF];
var gifsig = [0x47, 0x49, 0x46, 0x38, [0x37, 0x39], 0x61];
var swfsig = [[0x46, 0x43], 0x57, 0x53];
if (checksig (buffer, 0, Pngsig)) return imageinfopng (buffer);
if (checksig (buffer, 0, Jpgsig)) return imageinfojpg (buffer);
if (checksig (buffer, 0, Gifsig)) return imageinfogif (buffer);
if (checksig (buffer, 0, Swfsig)) return imageinfoswf (buffer);
return false;
};