手机版

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

时间:2021-10-16 来源:互联网 编辑:宝哥软件园 浏览:

遇到一个需求:假定有一个图片文件,真实的类型为jpg,而有人偷懒把使用联合图象专家组文件交换格式存储的编码图像文件扩展名直接复制一张,存为同名的巴布亚新几内亚文件,这样在as3读取文件时不会遇到问题,但手机c在读取文件时却遇到问题了- -!

现在就需要写一个程序,遍历所有文件夹下的文件,查找文件格式"不正常"的文件。我们的资源主要是gif、png、jpg、最开始,我到网上找到一篇文章:根据二进制流及文件头获取文件类型哑剧类型,然后读取文件二进制的头信息,获取其真实的文件类型,对与通过后缀名获得的文件类型进行比较。

复制代码代码如下: var FD=fs。OpenSync(new _ file _ path,' r ');var缓冲=新缓冲器(8);

var mineType=mime。查找(new _ file _ path);var fileType=mime。扩展名(mineType);

fs.readSync(fd,缓冲区,0,8,0);var newBuf=buffer.slice(0,4);var head_1=newBuf[0].toString(16);var head_2=newBuf[1].toString(16);var head_3=newBuf[2].toString(16);var head_4=newBuf[3].toString(16);var head _ iden=head _ 1 head _ 2;

var tempFileType=FILE _ TYPE _ CONFIG[head _ iden];if(!tempFileType){ head _ iden=head _ 3;

tempFileType=FILE _ TYPE _ CONFIG[head _ iden];

if(!tempFileType){ var msg=' Unknow fileType ' new _ file _ path '-' fileType;显示日志(消息);继续;}}

if (tempFileType!=文件类型){ var msg='错误文件类型new_file_path '-'文件类型|“tempFileType”-正确的图像文件格式;显示日志(消息);

g _ errorfiletyparr。推送(msg);}

后来搜索节点图像相关的信息时,找到这篇文章:node.js模块排名(图片)

然后筛选到一个模块"节点-imageinfo ",写了一个例子进行测试(故意把使用联合图象专家组文件交换格式存储的编码图像文件扩展名文件直接修改后缀名为巴布亚新几内亚):

它的源码,有兴趣可以研究一下:

复制代码代码如下:function readUInt32(缓冲区,偏移量,bigen Dian){ if(缓冲区。readUInt32){返回缓冲区。readUInt32(偏移量,bigen Dian);}

定义变量值;if(bigen Dian){ if(buffer。readuint 32 be){返回缓冲区。readuint 32 be(偏移量);}值=(缓冲区[偏移量] 24()缓冲区[偏移量1] 16()缓冲区[偏移量2] 8)缓冲区[偏移量3];} else { if(缓冲区。readuint32 le){返回缓冲区。readuint32 le(偏移量);}值=缓冲区[偏移量](缓冲区[偏移量1]8)(缓冲区[偏移量2]16)(缓冲区[偏移量3]24);}返回值;}

函数readUInt16(缓冲,偏移,bigen Dian){ if(缓冲。readUInt16){返回缓冲区。readUInt16(偏移量,bigen Dian);}

定义变量值;if(bigen Dian){ if(buffer。readuint16be){返回缓冲区。readuint16be(偏移量);} value=(buffer[offset]8)buffer[offset 1];} else { if(缓冲区。readuint16le){返回缓冲区。readuint16le(偏移量);}值=buffer[offset](缓冲区[offset 1]8);}返回值;}

函数readBit(缓冲区、偏移量、Bitoffset){ if(Bitoffset 7){ offset=math。地板(bitOffset/8);bitOffset=bitOffset % 8;}

var b=缓冲区[偏移量];if(bitOffset 7){ b=(7-bitOffset);}

var val=b0x01返回值;}

函数readBits(缓冲区,偏移量,bitOffset,bitLen,带符号){ var val=0;var neg=falseif(带符号){ if (readBit(缓冲区,偏移量,Bitoffset)0){ neg=true;} BitLen-;bitOffset}

定义变量字节=[];for(var I=0;i bitLeni ) { var b=readBit(缓冲区、偏移量、位偏移);if(i0(bitLen-I)% 8==0){ bytes。push(val);val=0;} val=1;val |=b;}字节。push(val);

val=新缓冲区(字节);瓦尔。否定=否定?真:假返回值;}

函数imageInfoPng(缓冲区){ var imageHeader=[0x49,0x48,0x44,0x52],pos=12

if(!checkSig(缓冲区、pos、imageHeader)){ 0返回false}

pos=4;返回{ type: 'image ',format: 'PNG ',mimeType: 'image/png ',width: readUInt32(buffer,pos,true),height: readUInt32(buffer,pos,true),};}

函数imageInfoJpg(buffer) { var pos=2,len=buffer.length,sizeSig=[0xff,[0xc0,0x C2]];

while(pos len){ if(CheckSig(buffer,pos,SiZeSig)){ pos=5;返回{ type: 'image ',format: 'JPG ',mimeType: 'image/jpeg ',width: readUInt16(buffer,pos 2,true),height: readUInt16(buffer,pos,true),};}

pos=2;var size=readUInt16(缓冲区,pos,true);pos=尺寸;}}

函数imageInfoGif(缓冲区){ var pos=6;

返回{ type: 'image ',format: 'GIF ',mimeType: 'image/gif ',width: readUInt16(buffer,pos,false),height: readUInt16(buffer,pos 2,false),};}

函数imageInfoSwf(缓冲区){ var pos=8,bitPos=0,val

if(缓冲区[0]===0x43) { try { //如果您有可用的兹利布(NPM)安装zlib),那么我们可以读取压缩的闪光文件buffer=require('zlib ').充气(缓冲片(8,100));pos=0;} catch (ex) { //无法获取压缩闪光文件的宽度/高度.然而(需要zlib)返回{ type: 'flash ',format: 'SWF ',mime type : ' application/x-冲击波-flash ',width: null,height: null,} }

var numBits=readBits(buffer,pos,bitPos,5)[0];bitPos=5;val=读取位(缓冲区、位置、位位置、数字位、真);var xMin=(numBits 9?readUInt16(val,0,true) : val[0]) * (val。负值?-1 : 1);bitPos=numBits

val=读取位(缓冲区、位置、位位置、数字位、真);var xMax=(numBits 9?readUInt16(val,0,true) : val[0]) * (val。负值?-1 : 1);bitPos=numBits

val=读取位(缓冲区、位置、位位置、数字位、真);var yMin=(numBits 9?readUInt16(val,0,true) : val[0]) * (val。负值?-1 : 1);bitPos=numBits

val=读取位(缓冲区、位置、位位置、数字位、真);var yMax=(numBits 9?readUInt16(val,0,true) : val[0]) * (val。负值?-1 : 1);

返回{ type: 'flash ',format: 'SWF ',mime type : ' application/x-冲击波-flash ',width : math。ceil((xMax-xMin)/20),高度:数学。ceil((yMax-yMin)/20)};}

函数checkSig(缓冲区、偏移量、SIG){ var len=SIG。长度;for(var I=0;我透镜;i ) { var b=缓冲区[i偏移量],s=sig[i],m=false

if(' number '==s的类型){ m=s===b;} else { for(var k in s){ var o=s[k];if(o===b){ m=true;} } }

if(!m){ 0返回false} }

返回真;}

模块。exports=函数imageInfo(缓冲区,路径){ var pngSig=[0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a];var jpgSig=[0xff,0xd8,0x ff];var gifSig=[0x47、0x49、0x46、0x38、[0x37、0x39]、0x 61];var swfSig=[[0x46,0x43],0x57,0x 53];

if (checkSig(缓冲区,0,pngSig))返回imageInfoPng(缓冲区);if (checkSig(缓冲区,0,jpgSig))返回imageInfoJpg(缓冲区);if (checkSig(缓冲区,0,gifSig))返回imageInfoGif(缓冲区);if (checkSig(缓冲区,0,swfSig))返回imageInfoSwf(缓冲区);

返回false };

版权声明:node.js解决获取图片真实文件类型的问题是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。