node-merge-images
Version:
Merges images. You can optionally set the merging orientation (vertical or horizontal), margins between images, etc.
2 lines (1 loc) • 8.72 kB
JavaScript
var t=require("fs"),e=require("path"),r=require("child_process"),i=require("events");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var o=n(t),s=n(e),a=n(r),u=n(i);var h=function(t,e){return t(e={exports:{}},e.exports),e.exports}((function(t,e){var r=u.default.EventEmitter;function i(t,e){var i={encoding:"utf8",timeout:0,maxBuffer:512e3,killSignal:"SIGKILL",output:null},n=arguments[arguments.length-1];if("function"!=typeof n&&(n=null),"object"==typeof arguments[2])for(var o=Object.keys(i),s=0;s<o.length;s++){var u=o[s];void 0!==arguments[2][u]&&(i[u]=arguments[2][u])}var h=a.default.spawn(t,e),c=!1,f=!1,p=function(t){this.proc=t,this.stderr=new d,t.emitter=new r,t.on=t.emitter.on.bind(t.emitter),this.out=t.emitter.emit.bind(t.emitter,"data"),this.err=this.stderr.out.bind(this.stderr),this.errCurrent=this.stderr.current.bind(this.stderr)};p.prototype.finish=function(t){this.proc.emitter.emit("end",t,this.errCurrent())};var d=function(t){this.stdout={contents:""},this.stderr={contents:""},this.callback=t;var e=function(t){return function(e){t.contents+=e,!c&&t.contents.length>i.maxBuffer&&(h.kill(i.killSignal),c=!0)}};this.out=e(this.stdout),this.err=e(this.stderr)};d.prototype.current=function(){return this.stdout.contents},d.prototype.errCurrent=function(){return this.stderr.contents},d.prototype.finish=function(t){this.callback(t,this.stdout.contents,this.stderr.contents)};var l,m=n?new d(n):new p(h);i.timeout>0&&(l=setTimeout((function(){c||(h.kill(i.killSignal),f=!0,c=!0,l=null)}),i.timeout)),h.stdout.setEncoding(i.encoding),h.stderr.setEncoding(i.encoding),h.stdout.addListener("data",(function(t){m.out(t,i.encoding)})),h.stderr.addListener("data",(function(t){m.err(t,i.encoding)}));var g=process.versions.node.split(".");return h.addListener(0==g[0]&&g[1]<7?"exit":"close",(function(t,e){if(l&&clearTimeout(l),0===t&&null===e)m.finish(null);else{var r=new Error("Command "+(f?"timed out":"failed")+": "+m.errCurrent());r.timedOut=f,r.killed=c,r.code=t,r.signal=e,m.finish(r)}})),h}function n(t){return t=t.split(/ /),new Date(t[0].replace(/:/g,"-")+" "+t[1]+" +0000")}function o(t){return t.replace(o.RE,(function(t){return 1===t.length?t.toLowerCase():t.substr(0,t.length-1).toLowerCase()+t.substr(t.length-1)}))}e.identify=function(t,r){var n,o=Array.isArray(t),s=o?[].concat(t):["-verbose",t];if("object"==typeof s[s.length-1]){if(n=!0,t=s[s.length-1],s[s.length-1]="-",!t.data)throw new Error('first argument is missing the "data" member')}else"function"==typeof t&&(s[s.length-1]="-",r=t);var a=i(e.identify.path,s,{timeout:12e4},(function(t,e,i){var n,s;t||(o?n=e:(s=(n=function(t){var e,r,i,n,o=t.split("\n"),s={},a=[s],u=0,h=[i];for(n in o.shift(),o)if((i=(e=o[n]).search(/\S/))>=0){for(r=e.split(": "),i>u&&h.push(i);i<u&&a.length;)h.pop(),s=a.pop(),u=h[h.length-1];r.length<2?(a.push(s),s=s[e.split(":")[0].trim().toLowerCase()]={}):s[r[0].trim().toLowerCase()]=r[1].trim(),u=i}return s}(e)).geometry.split(/x/),n.format=n.format.match(/\S*/)[0],n.width=parseInt(s[0]),n.height=parseInt(s[1]),n.depth=parseInt(n.depth),void 0!==n.quality&&(n.quality=parseInt(n.quality)/100))),r(t,n)}));return n&&("string"==typeof t.data?(a.stdin.setEncoding("binary"),a.stdin.write(t.data,"binary"),a.stdin.end()):a.stdin.end(t.data)),a},e.identify.path="identify",o.RE=/^[A-Z]+/;var s={bitsPerSample:Number,compression:Number,exifImageLength:Number,exifImageWidth:Number,exifOffset:Number,exposureProgram:Number,flash:Number,imageLength:Number,imageWidth:Number,isoSpeedRatings:Number,jpegInterchangeFormat:Number,jpegInterchangeFormatLength:Number,lightSource:Number,meteringMode:Number,orientation:Number,photometricInterpretation:Number,planarConfiguration:Number,resolutionUnit:Number,rowsPerStrip:Number,samplesPerPixel:Number,sensingMethod:Number,stripByteCounts:Number,subSecTime:Number,subSecTimeDigitized:Number,subSecTimeOriginal:Number,customRendered:Number,exposureMode:Number,focalLengthIn35mmFilm:Number,gainControl:Number,saturation:Number,sharpness:Number,subjectDistanceRange:Number,subSecTime:Number,subSecTimeDigitized:Number,subSecTimeOriginal:Number,whiteBalance:Number,sceneCaptureType:Number,dateTime:n,dateTimeDigitized:n,dateTimeOriginal:n};e.readMetadata=function(t,r){return e.identify(["-format","%[EXIF:*]",t],(function(t,e){var i={};t||e.split(/\n/).forEach((function(t){var e=t.indexOf("=");if(-1!==e){var r=t.substr(0,e).replace("/","-"),n=t.substr(e+1).trim(),a="default",u=r.indexOf(":");if(-1!==u&&(a=r.substr(0,u),r=r.substr(u+1),"exif"===a)){r=o(r);var h=s[r];h&&(n=h(n))}a in i?i[a][r]=n:i[a]={key:n}}})),r(t,i)}))},e.convert=function(t,r,n){var o={encoding:"binary"};return"function"==typeof r?(n=r,r=0):"number"!=typeof r&&(r=0),r&&(r=parseInt(r))>0&&!isNaN(r)&&(o.timeout=r),i(e.convert.path,t,o,n)},e.convert.path="convert";var h=function(t,r){var i=e.convert(t.args,t.opt.timeout,r);return t.opt.srcPath.match(/-$/)&&("string"==typeof t.opt.srcData?(i.stdin.setEncoding("binary"),i.stdin.write(t.opt.srcData,"binary"),i.stdin.end()):i.stdin.end(t.opt.srcData)),i};e.resize=function(t,r){var i=e.resizeArgs(t);return h(i,r)},e.crop=function(t,r){if("object"!=typeof t)throw new TypeError("First argument must be an object");if(!t.srcPath&&!t.srcData)throw new TypeError("No srcPath or data defined");if(!t.height&&!t.width)throw new TypeError("No width or height defined");if(t.srcPath)var i=t.srcPath;else i={data:t.srcData};e.identify(i,(function(i,n){if(i)return r&&r(i);var o=e.resizeArgs(t),s=!1,a=!1,u=[];o.args.forEach((function(e){if(!0===a&&(console.log("arg",e),a=!1),s||"-resize"==e||u.push(e),"-resize"==e&&(console.log("resize arg"),s=!0,a=!0),"-crop"===e&&(console.log("crop arg"),a=!0),"-resize"!=e&&s){var r=n.width/n.height<o.opt.width/o.opt.height?o.opt.width+"x":"x"+o.opt.height,i=t.gravity?t.gravity:"Center";u=u.concat(["-resize",r,"-gravity",i,"-crop",o.opt.width+"x"+o.opt.height+"+0+0","+repage"]),s=!1}})),o.args=u,h(o,r)}))},e.resizeArgs=function(t){var e={srcPath:null,srcData:null,srcFormat:null,dstPath:null,quality:.8,format:"jpg",progressive:!1,colorspace:null,width:0,height:0,strip:!0,filter:"Lagrange",sharpening:.2,customArgs:[],timeout:0};if("object"!=typeof t)throw new Error("first argument must be an object");for(var r in e)r in t&&(e[r]=t[r]);if(!e.srcPath&&!e.srcData)throw new Error("both srcPath and srcData are empty");if(e.format||(e.format="jpg"),e.srcPath||(e.srcPath=e.srcFormat?e.srcFormat+":-":"-"),e.dstPath||(e.dstPath=e.format?e.format+":-":"-"),0===e.width&&0===e.height)throw new Error("both width and height can not be 0 (zero)");var i=[e.srcPath];e.sharpening>0&&(i=i.concat(["-set","option:filter:blur",String(1-e.sharpening)])),e.filter&&(i.push("-filter"),i.push(e.filter)),e.strip&&i.push("-strip"),(e.width||e.height)&&(i.push("-resize"),0===e.height?i.push(String(e.width)):0===e.width?i.push("x"+String(e.height)):i.push(String(e.width)+"x"+String(e.height))),e.format=e.format.toLowerCase();var n="jpg"===e.format||"jpeg"===e.format;return n&&e.progressive&&(i.push("-interlace"),i.push("plane")),n||"png"===e.format?(i.push("-quality"),i.push(Math.round(100*e.quality).toString())):"miff"!==e.format&&"mif"!==e.format||(i.push("-quality"),i.push(Math.round(9*e.quality).toString())),e.colorspace&&(i.push("-colorspace"),i.push(e.colorspace)),Array.isArray(e.customArgs)&&e.customArgs.length&&(i=i.concat(e.customArgs)),i.push(e.dstPath),{opt:e,args:i}}}));h.identify,h.readMetadata,h.convert,h.resize,h.crop,h.resizeArgs;const c=require("fs-extra");module.exports=async(t,e,r={})=>new Promise(((i,n)=>{try{r=Object.assign({direction:"vertical",background:"white",offset:0},r),function(t,e,r){if(!Array.isArray(t))throw new TypeError("inputPaths must be an array that contains images");if(t.length<1)throw new TypeError("At least inputPaths must contain more than one image");if(!e)throw new TypeError("outputPath should be a file path");if(!/^(vertical|horizontal)$/.test(r.direction))throw new TypeError('The direction option should be "vertical" or "horizontal"');if(r.offset<0)throw new TypeError("The offset option should be a number greater than or equal to 0");const i=t.filter((t=>!o.default.existsSync(t)));if(i.length>0)throw new TypeError(`Input path ${i.join(", ")} not found`)}(t,e,r);const a=[];a.push("-background"),a.push(r.background),a.push("-size"),a.push("vertical"===r.direction?`x${r.offset}`:`${r.offset}x`),a.push(t[0]);for(let e of t.slice(1))r.offset>0&&a.push("xc:none"),a.push(e);a.push("vertical"===r.direction?"-append":"+append"),a.push(e),o.default.existsSync(s.default.dirname(e))||(c.mkdirsSync(s.default.dirname(e)),o.default.chmodSync(s.default.dirname(e),493)),h.convert(a,(t=>{t?n(t):i()}))}catch(t){n(t)}}));
;