mirror of https://gitgud.io/fatchan/gm
parent
698e99d7e2
commit
f60627d226
9 changed files with 495 additions and 376 deletions
@ -1,34 +1,55 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
module.exports = gm |
||||
/** |
||||
* Constructor. |
||||
* |
||||
* @param {String|Number} path - path to img source or width of img to create |
||||
* @param {Number} [height] - optional height of img to create |
||||
* @param {String} [color] - optional hex background color of created img |
||||
*/ |
||||
|
||||
function gm (source, height, color) { |
||||
var width; |
||||
|
||||
if (!(this instanceof gm)) { |
||||
return new gm(source, height, color); |
||||
} |
||||
|
||||
this.data = {}; |
||||
this._in = []; |
||||
this._out = []; |
||||
|
||||
function gm(source, height, color){ |
||||
if (!(this instanceof gm)) |
||||
return new gm(source, height, color) |
||||
if (height) { |
||||
// new images
|
||||
width = source; |
||||
source = ""; |
||||
|
||||
this.data = {} |
||||
this._in = [] |
||||
this._out = [] |
||||
var arg = ["-size", width + "x" + height]; |
||||
|
||||
var width |
||||
if (color) { |
||||
arg = arg.concat(['"xc:'+ color + '"']); |
||||
} |
||||
|
||||
if (height){ |
||||
// new images
|
||||
width = source |
||||
source = "" |
||||
this.arg( |
||||
[ "-size", width + "x" + height ] |
||||
.concat( color ? ['"xc:'+ color + '"'] : [] ) |
||||
) |
||||
this.arg(arg); |
||||
} |
||||
|
||||
this.source = source |
||||
this.source = source; |
||||
} |
||||
|
||||
require("./lib/getters")(gm.prototype) |
||||
require("./lib/args")(gm.prototype) |
||||
require("./lib/drawing")(gm.prototype) |
||||
require("./lib/convenience")(gm.prototype) |
||||
require("./lib/command")(gm.prototype) |
||||
|
||||
/** |
||||
* Augment the prototype. |
||||
*/ |
||||
|
||||
require("./lib/getters")(gm.prototype); |
||||
require("./lib/args")(gm.prototype); |
||||
require("./lib/drawing")(gm.prototype); |
||||
require("./lib/convenience")(gm.prototype); |
||||
require("./lib/command")(gm.prototype); |
||||
|
||||
/** |
||||
* Expose. |
||||
*/ |
||||
|
||||
module.exports = gm; |
||||
|
||||
|
@ -1,10 +1,8 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
module.exports = function(proto){ |
||||
|
||||
require("./convenience/thumb")(proto) |
||||
require("./convenience/morph")(proto) |
||||
require("./convenience/sepia")(proto) |
||||
|
||||
module.exports = function (proto) { |
||||
require("./convenience/thumb")(proto); |
||||
require("./convenience/morph")(proto); |
||||
require("./convenience/sepia")(proto); |
||||
} |
||||
|
@ -1,30 +1,48 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
var fs = require('fs') |
||||
var fs = require('fs'); |
||||
|
||||
module.exports = function (proto) { |
||||
|
||||
function noop () {} |
||||
|
||||
module.exports = function(proto){ |
||||
|
||||
// http://www.graphicsmagick.org/GraphicsMagick.html#details-morph
|
||||
proto.morph = function (other, outname, callback){ |
||||
if (!callback) callback = function(){} |
||||
if (!outname) throw new Error("an output filename is required") |
||||
var self = this |
||||
self.arg(null, [other, "-morph", 1]) |
||||
self.write(outname, function(err, stdout, stderr, cmd){ |
||||
if (err) return callback.call(self, err, stdout, stderr, cmd) |
||||
var remaining = 3 |
||||
fs.unlink(outname + ".0", next) |
||||
fs.unlink(outname + ".2", next) |
||||
fs.rename(outname + ".1", outname, next) |
||||
function next(err){ |
||||
proto.morph = function morph (other, outname, callback) { |
||||
if (!callback) callback = noop; |
||||
|
||||
if (!outname) { |
||||
throw new Error("an output filename is required"); |
||||
} |
||||
|
||||
var self = this; |
||||
|
||||
self.arg(null, [other, "-morph", 1]); |
||||
|
||||
self.write(outname, function (err, stdout, stderr, cmd) { |
||||
if (err) { |
||||
return callback.call(self, err, stdout, stderr, cmd); |
||||
} |
||||
|
||||
var remaining = 3; |
||||
|
||||
fs.unlink(outname + ".0", next); |
||||
fs.unlink(outname + ".2", next); |
||||
fs.rename(outname + ".1", outname, next); |
||||
|
||||
function next (err) { |
||||
if (next.err) return; |
||||
|
||||
if (err) { |
||||
remaining = 0 |
||||
return callback.call(self, err, stdout, stderr, cmd) |
||||
next.err = err; |
||||
return callback.call(self, err, stdout, stderr, cmd); |
||||
} |
||||
if (!--remaining) |
||||
return callback.call(self, err, stdout, stderr, cmd) |
||||
|
||||
if (--remaining) return; |
||||
|
||||
callback.call(self, err, stdout, stderr, cmd); |
||||
} |
||||
}) |
||||
return self |
||||
}); |
||||
|
||||
return self; |
||||
}} |
||||
|
@ -1,10 +1,8 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
module.exports = function(proto){ |
||||
|
||||
proto.sepia = function(){ |
||||
return this.modulate(115, 0, 100).colorize(7, 21, 50)
|
||||
module.exports = function (proto) { |
||||
proto.sepia = function sepia () { |
||||
return this.modulate(115, 0, 100).colorize(7, 21, 50); |
||||
} |
||||
|
||||
} |
||||
|
@ -1,51 +1,57 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
module.exports = function(proto){ |
||||
|
||||
proto.thumb = function (w, h, name, quality, callback){ |
||||
module.exports = function (proto) { |
||||
|
||||
proto.thumb = function thumb (w, h, name, quality, callback) { |
||||
var self = this |
||||
, args = Array.prototype.slice.call(arguments, 0) |
||||
callback = args.pop() |
||||
w = args.shift() |
||||
h = args.shift() |
||||
name = args.shift() |
||||
quality = args.shift() || 63 |
||||
self.size(function(err, size){ |
||||
if (err) return callback.apply(self, arguments) |
||||
w = parseInt(w, 10) |
||||
h = parseInt(h, 10) |
||||
var w1, h1 |
||||
if (size.width < size.height){ |
||||
w1 = w |
||||
h1 = Math.floor(size.height * (w/size.width)) |
||||
if (h1 < h){ |
||||
w1 = Math.floor(w1 * ( ((h-h1)/h)+1 )) |
||||
h1 = h |
||||
, args = Array.prototype.slice.call(arguments); |
||||
|
||||
callback = args.pop(); |
||||
w = args.shift(); |
||||
h = args.shift(); |
||||
name = args.shift(); |
||||
quality = args.shift() || 63; |
||||
|
||||
self.size(function (err, size) { |
||||
if (err) { |
||||
return callback.apply(self, arguments); |
||||
} |
||||
|
||||
w = parseInt(w, 10); |
||||
h = parseInt(h, 10); |
||||
|
||||
var w1, h1; |
||||
|
||||
if (size.width < size.height) { |
||||
w1 = w; |
||||
h1 = Math.floor(size.height * (w/size.width)); |
||||
if (h1 < h) { |
||||
w1 = Math.floor(w1 * (((h-h1)/h) + 1)); |
||||
h1 = h; |
||||
} |
||||
}
|
||||
else if (size.width > size.height){ |
||||
h1 = h |
||||
w1 = Math.floor(size.width * (h/size.height)) |
||||
if (w1 < w){ |
||||
h1 = Math.floor(h1 * ( ((w-w1)/w)+1 )) |
||||
w1 = w |
||||
} else if (size.width > size.height) { |
||||
h1 = h; |
||||
w1 = Math.floor(size.width * (h/size.height)); |
||||
if (w1 < w) { |
||||
h1 = Math.floor(h1 * (((w-w1)/w) + 1)); |
||||
w1 = w; |
||||
} |
||||
} else if (size.width == size.height) { |
||||
w1 = w; |
||||
h1 = h; |
||||
} |
||||
else if (size.width == size.height){ |
||||
w1 = w |
||||
h1 = h |
||||
} |
||||
|
||||
self |
||||
.quality(quality) |
||||
.arg(["-size", w1+"x"+h1]) |
||||
.scale(w1, h1) |
||||
.crop(w, h) |
||||
.noProfile() |
||||
.write(name, function(){ |
||||
callback.apply(self, arguments) |
||||
}) |
||||
}) |
||||
return self |
||||
.quality(quality) |
||||
.arg(["-size", w1+"x"+h1]) |
||||
.scale(w1, h1) |
||||
.crop(w, h) |
||||
.noProfile() |
||||
.write(name, function () { |
||||
callback.apply(self, arguments) |
||||
}); |
||||
}); |
||||
|
||||
return self; |
||||
}} |
||||
|
Loading…
Reference in new issue