Created
September 26, 2013 21:31
-
-
Save brettcvz/6720871 to your computer and use it in GitHub Desktop.
Revisions
-
brettcvz created this gist
Sep 26, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ //To use this, in another file call //require("./gm-composite.js")(gm.prototype); //You can then do gm("baseImage.png").composite(gm("watermark.png"), "SouthEast", function(err, composite_gm_obj){...}); var gm = require('gm'); var randInt = function(min, max) { return Math.floor(min + (Math.random() * (max-min))); }; var randString = function(len) { len = len || 16; var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; var ret = ""; while(ret.length < len) { ret += chars[randInt(0, chars.length)]; } return ret; }; module.exports = function(proto) { proto.compose = function(other, gravity, callback) { // We have potentially two streams to deal with, so we first write the watermark to a file, // then pull the base image from stdin and write to stdout // gm composite -resize 400x400 -gravity center Watermark.png test_image.png test_output.png var path = "/tmp/watermark_"+randString(); var prev = { _subCommand: this._subCommand, _out: this._out.slice(), //copy _in: this._in.slice() }; var curr = this; other.write(path, function(err) { curr.subCommand("composite"); curr.gravity(gravity); curr.in(path); var composite = gm(curr.stream()); //re-applying other properties composite._subCommand = prev._subCommand; composite._out = prev._out; composite._in = prev._in; callback(null, composite); }); return this; }; };