node.js解决获取图片真实文件类型的问题(3)

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;
};

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxxyp.html