Nodejs Recognizes Picture Types
It is inaccurate to obtain the image format by cutting the suffix of the file name, because the file suffix can be changed forcibly, and such a. gif picture can also be saved as. jpg.
So how do we do file type checking on Node?
Judging Picture Format by File Header Identification
In fact, it is very simple, each picture file has a header identification, the header identification of various formats of the image is different, so you can identify the image format by judging the header identification.
By searching for information on the Internet, the following header labels are summarized:
1.JPEG/JPG - File Header Identification (2 bytes): ff, d8 File End Identification (2 bytes): ff, d9
2.TGA - uncompressed first 5 bytes 00 02 00 - RLE compressed first 5 bytes 00 100 00
3.PNG - File Header Identification (8 bytes) 89 504E 470D 0A 1A 0A
4.GIF - File Header Identification (6 bytes) 47 49 46 38 39(37) 61
5.BMP - File Header Identification (2 bytes) 424D B M
6.PCX - File Header Identification (1 bytes) 0A
7.TIFF - File Header Identification (2 bytes) 4D 4D or 49 49
8.ICO - File Header Identification (8 bytes) 00 01 00 01 00 20 20
9.CUR - File Header Identification (8 bytes) 00 00 02 00 01 00 20
10.IFF - File Header Identification (4 bytes) 46 4F 52 4D
11.ANI - File Header Identification (4 bytes) 52 49 46 46 46
Knowledge Points: 1 bytes = 8 bits, the above numbers are all hexadecimal, which takes up 4 bits space, and each two hexadecimal digits takes up one byte.
How to Judge
The image format can be determined by comparing the binary stream of the image resource with the identifier.
Because the implementation logic is very simple, the specific implementation logic just look at the code and annotations, here to contribute to you for reference, convenient for you to develop and use.
function getImageSuffix(fileBuffer) { // Byte the file identifier header mentioned above into an array const imageBufferHeaders = [ { bufBegin: [0xff, 0xd8], bufEnd: [0xff, 0xd9], suffix: '.jpg' }, { bufBegin: [0x00, 0x00, 0x02, 0x00, 0x00], suffix: '.tga' }, { bufBegin: [0x00, 0x00, 0x10, 0x00, 0x00], suffix: '.rle' }, { bufBegin: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], suffix: '.png' }, { bufBegin: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], suffix: '.gif' }, { bufBegin: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], suffix: '.gif' }, { bufBegin: [0x42, 0x4d], suffix: '.bmp' }, { bufBegin: [0x0a], suffix: '.pcx' }, { bufBegin: [0x49, 0x49], suffix: '.tif' }, { bufBegin: [0x4d, 0x4d], suffix: '.tif' }, { bufBegin: [0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x20], suffix: '.ico' }, { bufBegin: [0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x20, 0x20], suffix: '.cur' }, { bufBegin: [0x46, 0x4f, 0x52, 0x4d], suffix: '.iff' }, { bufBegin: [0x52, 0x49, 0x46, 0x46], suffix: '.ani' } ] for (const imageBufferHeader of imageBufferHeaders) { let isEqual // Determine the header prefix of the logo if (imageBufferHeader.bufBegin) { const buf = Buffer.from(imageBufferHeader.bufBegin) isEqual = buf.equals( //Using buffer.slice method to cut buffers in bytes fileBuffer.slice(0, imageBufferHeader.bufBegin.length) ) } // Determine the header suffix of the logo if (isEqual && imageBufferHeader.bufEnd) { const buf = Buffer.from(imageBufferHeader.bufEnd) isEqual = buf.equals(fileBuffer.slice(-imageBufferHeader.bufEnd.length)) } if (isEqual) { return imageBufferHeader.suffix } } // Failed to recognize the file type return '' }
In this way, we can accurately identify the image format in Node.
The article is very short. I hope it can help you.~