-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageResizer.min.js
1 lines (1 loc) · 13.1 KB
/
ImageResizer.min.js
1
var ImageResizer=function(){"use strict";var e={};(function(){function e(e){return!!e.exifdata}function t(e,t){t=t||e.match(/^data\:([^\;]+)\;base64,/im)[1]||"",e=e.replace(/^data\:([^\;]+)\;base64,/gim,"");for(var n=atob(e),r=n.length,i=new ArrayBuffer(r),o=new Uint8Array(i),a=0;r>a;a++)o[a]=n.charCodeAt(a);return i}function n(e,t){var n=new XMLHttpRequest;n.open("GET",e,!0),n.responseType="blob",n.onload=function(e){(200==this.status||0===this.status)&&t(this.response)},n.send()}function r(e,r){function a(t){var n=i(t),a=o(t);e.exifdata=n||{},e.iptcdata=a||{},r&&r.call(e)}if(e.src)if(/^data\:/i.test(e.src)){var s=t(e.src);a(s)}else if(/^blob\:/i.test(e.src)){var d=new FileReader;d.onload=function(e){a(e.target.result)},n(e.src,function(e){d.readAsArrayBuffer(e)})}else{var u=new XMLHttpRequest;u.onload=function(){if(200!=this.status&&0!==this.status)throw"Could not load image";a(u.response),u=null},u.open("GET",e.src,!0),u.responseType="arraybuffer",u.send(null)}else if(window.FileReader&&(e instanceof window.Blob||e instanceof window.File)){var d=new FileReader;d.onload=function(e){c&&console.log("Got file of length "+e.target.result.byteLength),a(e.target.result)},d.readAsArrayBuffer(e)}}function i(e){var t=new DataView(e);if(c&&console.log("Got file of length "+e.byteLength),255!=t.getUint8(0)||216!=t.getUint8(1))return c&&console.log("Not a valid JPEG"),!1;for(var n,r=2,i=e.byteLength;i>r;){if(255!=t.getUint8(r))return c&&console.log("Not a valid marker at offset "+r+", found: "+t.getUint8(r)),!1;if(n=t.getUint8(r+1),c&&console.log(n),225==n)return c&&console.log("Found 0xFFE1 marker"),l(t,r+4,t.getUint16(r+2)-2);r+=2+t.getUint16(r+2)}}function o(e){var t=new DataView(e);if(c&&console.log("Got file of length "+e.byteLength),255!=t.getUint8(0)||216!=t.getUint8(1))return c&&console.log("Not a valid JPEG"),!1;for(var n=2,r=e.byteLength,i=function(e,t){return 56===e.getUint8(t)&&66===e.getUint8(t+1)&&73===e.getUint8(t+2)&&77===e.getUint8(t+3)&&4===e.getUint8(t+4)&&4===e.getUint8(t+5)};r>n;){if(i(t,n)){var o=t.getUint8(n+7);o%2!==0&&(o+=1),0===o&&(o=4);var s=n+8+o,d=t.getUint16(n+6+o);return a(e,s,d)}n++}}function a(e,t,n){for(var r,i,o,a,s,d=new DataView(e),l={},c=t;t+n>c;)28===d.getUint8(c)&&2===d.getUint8(c+1)&&(a=d.getUint8(c+2),a in w&&(o=d.getInt16(c+3),s=o+5,i=w[a],r=u(d,c+5,o),l.hasOwnProperty(i)?l[i]instanceof Array?l[i].push(r):l[i]=[l[i],r]:l[i]=r)),c++;return l}function s(e,t,n,r,i){var o,a,s,u=e.getUint16(n,!i),l={};for(s=0;u>s;s++)o=n+12*s+2,a=r[e.getUint16(o,!i)],!a&&c&&console.log("Unknown tag: "+e.getUint16(o,!i)),l[a]=d(e,o,t,n,i);return l}function d(e,t,n,r,i){var o,a,s,d,l,c,g=e.getUint16(t+2,!i),h=e.getUint32(t+4,!i),f=e.getUint32(t+8,!i)+n;switch(g){case 1:case 7:if(1==h)return e.getUint8(t+8,!i);for(o=h>4?f:t+8,a=[],d=0;h>d;d++)a[d]=e.getUint8(o+d);return a;case 2:return o=h>4?f:t+8,u(e,o,h-1);case 3:if(1==h)return e.getUint16(t+8,!i);for(o=h>2?f:t+8,a=[],d=0;h>d;d++)a[d]=e.getUint16(o+2*d,!i);return a;case 4:if(1==h)return e.getUint32(t+8,!i);for(a=[],d=0;h>d;d++)a[d]=e.getUint32(f+4*d,!i);return a;case 5:if(1==h)return l=e.getUint32(f,!i),c=e.getUint32(f+4,!i),s=new Number(l/c),s.numerator=l,s.denominator=c,s;for(a=[],d=0;h>d;d++)l=e.getUint32(f+8*d,!i),c=e.getUint32(f+4+8*d,!i),a[d]=new Number(l/c),a[d].numerator=l,a[d].denominator=c;return a;case 9:if(1==h)return e.getInt32(t+8,!i);for(a=[],d=0;h>d;d++)a[d]=e.getInt32(f+4*d,!i);return a;case 10:if(1==h)return e.getInt32(f,!i)/e.getInt32(f+4,!i);for(a=[],d=0;h>d;d++)a[d]=e.getInt32(f+8*d,!i)/e.getInt32(f+4+8*d,!i);return a}}function u(e,t,n){for(var r="",i=t;t+n>i;i++)r+=String.fromCharCode(e.getUint8(i));return r}function l(e,t){if("Exif"!=u(e,t,4))return c&&console.log("Not valid EXIF data! "+u(e,t,4)),!1;var n,r,i,o,a,d=t+6;if(18761==e.getUint16(d))n=!1;else{if(19789!=e.getUint16(d))return c&&console.log("Not valid TIFF data! (no 0x4949 or 0x4D4D)"),!1;n=!0}if(42!=e.getUint16(d+2,!n))return c&&console.log("Not valid TIFF data! (no 0x002A)"),!1;var l=e.getUint32(d+4,!n);if(8>l)return c&&console.log("Not valid TIFF data! (First offset less than 8)",e.getUint32(d+4,!n)),!1;if(r=s(e,d,d+l,m,n),r.ExifIFDPointer){o=s(e,d,d+r.ExifIFDPointer,f,n);for(i in o){switch(i){case"LightSource":case"Flash":case"MeteringMode":case"ExposureProgram":case"SensingMethod":case"SceneCaptureType":case"SceneType":case"CustomRendered":case"WhiteBalance":case"GainControl":case"Contrast":case"Saturation":case"Sharpness":case"SubjectDistanceRange":case"FileSource":o[i]=S[i][o[i]];break;case"ExifVersion":case"FlashpixVersion":o[i]=String.fromCharCode(o[i][0],o[i][1],o[i][2],o[i][3]);break;case"ComponentsConfiguration":o[i]=S.Components[o[i][0]]+S.Components[o[i][1]]+S.Components[o[i][2]]+S.Components[o[i][3]]}r[i]=o[i]}}if(r.GPSInfoIFDPointer){a=s(e,d,d+r.GPSInfoIFDPointer,p,n);for(i in a){switch(i){case"GPSVersionID":a[i]=a[i][0]+"."+a[i][1]+"."+a[i][2]+"."+a[i][3]}r[i]=a[i]}}return r}var c=!1,g=this,h=function(e){return e instanceof h?e:this instanceof h?void(this.EXIFwrapped=e):new h(e)};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=h),exports.EXIF=h):g.EXIF=h;var f=h.Tags={36864:"ExifVersion",40960:"FlashpixVersion",40961:"ColorSpace",40962:"PixelXDimension",40963:"PixelYDimension",37121:"ComponentsConfiguration",37122:"CompressedBitsPerPixel",37500:"MakerNote",37510:"UserComment",40964:"RelatedSoundFile",36867:"DateTimeOriginal",36868:"DateTimeDigitized",37520:"SubsecTime",37521:"SubsecTimeOriginal",37522:"SubsecTimeDigitized",33434:"ExposureTime",33437:"FNumber",34850:"ExposureProgram",34852:"SpectralSensitivity",34855:"ISOSpeedRatings",34856:"OECF",37377:"ShutterSpeedValue",37378:"ApertureValue",37379:"BrightnessValue",37380:"ExposureBias",37381:"MaxApertureValue",37382:"SubjectDistance",37383:"MeteringMode",37384:"LightSource",37385:"Flash",37396:"SubjectArea",37386:"FocalLength",41483:"FlashEnergy",41484:"SpatialFrequencyResponse",41486:"FocalPlaneXResolution",41487:"FocalPlaneYResolution",41488:"FocalPlaneResolutionUnit",41492:"SubjectLocation",41493:"ExposureIndex",41495:"SensingMethod",41728:"FileSource",41729:"SceneType",41730:"CFAPattern",41985:"CustomRendered",41986:"ExposureMode",41987:"WhiteBalance",41988:"DigitalZoomRation",41989:"FocalLengthIn35mmFilm",41990:"SceneCaptureType",41991:"GainControl",41992:"Contrast",41993:"Saturation",41994:"Sharpness",41995:"DeviceSettingDescription",41996:"SubjectDistanceRange",40965:"InteroperabilityIFDPointer",42016:"ImageUniqueID"},m=h.TiffTags={256:"ImageWidth",257:"ImageHeight",34665:"ExifIFDPointer",34853:"GPSInfoIFDPointer",40965:"InteroperabilityIFDPointer",258:"BitsPerSample",259:"Compression",262:"PhotometricInterpretation",274:"Orientation",277:"SamplesPerPixel",284:"PlanarConfiguration",530:"YCbCrSubSampling",531:"YCbCrPositioning",282:"XResolution",283:"YResolution",296:"ResolutionUnit",273:"StripOffsets",278:"RowsPerStrip",279:"StripByteCounts",513:"JPEGInterchangeFormat",514:"JPEGInterchangeFormatLength",301:"TransferFunction",318:"WhitePoint",319:"PrimaryChromaticities",529:"YCbCrCoefficients",532:"ReferenceBlackWhite",306:"DateTime",270:"ImageDescription",271:"Make",272:"Model",305:"Software",315:"Artist",33432:"Copyright"},p=h.GPSTags={0:"GPSVersionID",1:"GPSLatitudeRef",2:"GPSLatitude",3:"GPSLongitudeRef",4:"GPSLongitude",5:"GPSAltitudeRef",6:"GPSAltitude",7:"GPSTimeStamp",8:"GPSSatellites",9:"GPSStatus",10:"GPSMeasureMode",11:"GPSDOP",12:"GPSSpeedRef",13:"GPSSpeed",14:"GPSTrackRef",15:"GPSTrack",16:"GPSImgDirectionRef",17:"GPSImgDirection",18:"GPSMapDatum",19:"GPSDestLatitudeRef",20:"GPSDestLatitude",21:"GPSDestLongitudeRef",22:"GPSDestLongitude",23:"GPSDestBearingRef",24:"GPSDestBearing",25:"GPSDestDistanceRef",26:"GPSDestDistance",27:"GPSProcessingMethod",28:"GPSAreaInformation",29:"GPSDateStamp",30:"GPSDifferential"},S=h.StringValues={ExposureProgram:{0:"Not defined",1:"Manual",2:"Normal program",3:"Aperture priority",4:"Shutter priority",5:"Creative program",6:"Action program",7:"Portrait mode",8:"Landscape mode"},MeteringMode:{0:"Unknown",1:"Average",2:"CenterWeightedAverage",3:"Spot",4:"MultiSpot",5:"Pattern",6:"Partial",255:"Other"},LightSource:{0:"Unknown",1:"Daylight",2:"Fluorescent",3:"Tungsten (incandescent light)",4:"Flash",9:"Fine weather",10:"Cloudy weather",11:"Shade",12:"Daylight fluorescent (D 5700 - 7100K)",13:"Day white fluorescent (N 4600 - 5400K)",14:"Cool white fluorescent (W 3900 - 4500K)",15:"White fluorescent (WW 3200 - 3700K)",17:"Standard light A",18:"Standard light B",19:"Standard light C",20:"D55",21:"D65",22:"D75",23:"D50",24:"ISO studio tungsten",255:"Other"},Flash:{0:"Flash did not fire",1:"Flash fired",5:"Strobe return light not detected",7:"Strobe return light detected",9:"Flash fired, compulsory flash mode",13:"Flash fired, compulsory flash mode, return light not detected",15:"Flash fired, compulsory flash mode, return light detected",16:"Flash did not fire, compulsory flash mode",24:"Flash did not fire, auto mode",25:"Flash fired, auto mode",29:"Flash fired, auto mode, return light not detected",31:"Flash fired, auto mode, return light detected",32:"No flash function",65:"Flash fired, red-eye reduction mode",69:"Flash fired, red-eye reduction mode, return light not detected",71:"Flash fired, red-eye reduction mode, return light detected",73:"Flash fired, compulsory flash mode, red-eye reduction mode",77:"Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected",79:"Flash fired, compulsory flash mode, red-eye reduction mode, return light detected",89:"Flash fired, auto mode, red-eye reduction mode",93:"Flash fired, auto mode, return light not detected, red-eye reduction mode",95:"Flash fired, auto mode, return light detected, red-eye reduction mode"},SensingMethod:{1:"Not defined",2:"One-chip color area sensor",3:"Two-chip color area sensor",4:"Three-chip color area sensor",5:"Color sequential area sensor",7:"Trilinear sensor",8:"Color sequential linear sensor"},SceneCaptureType:{0:"Standard",1:"Landscape",2:"Portrait",3:"Night scene"},SceneType:{1:"Directly photographed"},CustomRendered:{0:"Normal process",1:"Custom process"},WhiteBalance:{0:"Auto white balance",1:"Manual white balance"},GainControl:{0:"None",1:"Low gain up",2:"High gain up",3:"Low gain down",4:"High gain down"},Contrast:{0:"Normal",1:"Soft",2:"Hard"},Saturation:{0:"Normal",1:"Low saturation",2:"High saturation"},Sharpness:{0:"Normal",1:"Soft",2:"Hard"},SubjectDistanceRange:{0:"Unknown",1:"Macro",2:"Close view",3:"Distant view"},FileSource:{3:"DSC"},Components:{0:"",1:"Y",2:"Cb",3:"Cr",4:"R",5:"G",6:"B"}},w={120:"caption",110:"credit",25:"keywords",55:"dateCreated",80:"byline",85:"bylineTitle",122:"captionWriter",105:"headline",116:"copyright",15:"category"};h.getData=function(t,n){return(t instanceof Image||t instanceof HTMLImageElement)&&!t.complete?!1:(e(t)?n&&n.call(t):r(t,n),!0)},h.getTag=function(t,n){return e(t)?t.exifdata[n]:void 0},h.getAllTags=function(t){if(!e(t))return{};var n,r=t.exifdata,i={};for(n in r)r.hasOwnProperty(n)&&(i[n]=r[n]);return i},h.pretty=function(t){if(!e(t))return"";var n,r=t.exifdata,i="";for(n in r)r.hasOwnProperty(n)&&(i+="object"==typeof r[n]?r[n]instanceof Number?n+" : "+r[n]+" ["+r[n].numerator+"/"+r[n].denominator+"]\r\n":n+" : ["+r[n].length+" values]\r\n":n+" : "+r[n]+"\r\n");return i},h.readFromBinaryFile=function(e){return i(e)},"function"==typeof define&&define.amd&&define("exif-js",[],function(){return h})}).call(e);var e=this,t={returnType:"image"},n=function(){var e=new Image;return e.src=URL.createObjectURL(t.image),e},r=function(e){var t=new Image;return t.src=e.toDataURL("image/png"),t},i=function(e){for(var t=e.toDataURL(),n=atob(t.split(",")[1]),r=[],i=0;i<n.length;i++)r.push(n.charCodeAt(i));var o=new Blob([new Uint8Array(r)],{type:"image/png"});return o},o=function(e,n){"image"===t.returnType?n.resolve(r(e)):n.resolve(i(e))},a=function(e,n,r){var i=document.createElement("canvas"),a=i.getContext("2d"),s=e.getContext("2d");switch(i.width=e.width,i.height=e.height,r){case 2:a.transform(-1,0,0,1,e.width,0);break;case 3:a.transform(-1,0,0,-1,e.width,height);break;case 4:a.transform(1,0,0,-1,0,e.height);break;case 5:i.width=e.height,i.height=e.width,a.transform(0,1,1,0,0,0);break;case 6:i.width=e.height,i.height=e.width,a.transform(0,1,-1,0,e.height,0);break;case 7:i.width=e.height,i.height=e.width,a.transform(0,-1,-1,0,e.height,e.width);break;case 8:i.width=e.height,i.height=e.width,a.transform(0,-1,1,0,0,e.width)}a.drawImage(e,0,0);var d=t.width,u=t.height;d&&!u&&(t.ratio=d/i.width>1?1:d/i.width),u&&!d&&(t.ratio=u/i.height>1?1:u/i.height),t.ratio&&(d=i.width*t.ratio,u=i.height*t.ratio),e.width=d,e.height=u,s.drawImage(i,0,0,i.width,i.height,0,0,e.width,e.height),o(e,n)},s=function(t,n){var r=document.createElement("canvas"),i=r.getContext("2d");t.onload=function(o){e.EXIF.getData(t,function(){var o=e.EXIF.getAllTags(this);r.width=t.width,r.height=t.height,i.drawImage(t,0,0,r.width,r.height),a(r,n,o.Orientation)})}},d=function(e){var t=n();s(t,e)},u=function(e){var n=new Image;n.src=t.image.src,s(n,e)},l={resize:function(e){if(!e.image)throw"require image";if(!e.ratio&&!e.width&&!e.height)throw"require image size";t=e;var n=$q.defer();if(e.image instanceof File)d(n);else if(e.image instanceof Image||e.image instanceof HTMLImageElement)u(n);else{if(!(e.image instanceof HTMLCanvasElement))throw"invalid image type";a(e.image,n)}return n.promise}};return l}();