Skip to content

Instantly share code, notes, and snippets.

@Graniron
Created October 4, 2017 06:13
Show Gist options
  • Save Graniron/36955b4bb10cc72afb1d49eb396756ea to your computer and use it in GitHub Desktop.
Save Graniron/36955b4bb10cc72afb1d49eb396756ea to your computer and use it in GitHub Desktop.
Get file type from array buffer(based on hexadecimal/magic-number value)
function getImageType(arrayBuffer){
var type = "";
var dv = new DataView(arrayBuffer,0,5);
var nume1 = dv.getUint8(0,true);
var nume2 = dv.getUint8(1,true);
var hex = nume1.toString(16) + nume2.toString(16) ;
switch(hex){
case "8950":
type = "image/png";
break;
case "4749":
type = "image/gif";
break;
case "424d":
type = "image/bmp";
break;
case "ffd8":
type = "image/jpeg";
break;
default:
type = null;
break;
}
return type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment