master
Aaron Heckmann 13 years ago
parent 698e99d7e2
commit f60627d226
  1. 67
      index.js
  2. 289
      lib/args.js
  3. 66
      lib/command.js
  4. 10
      lib/convenience.js
  5. 60
      lib/convenience/morph.js
  6. 8
      lib/convenience/sepia.js
  7. 88
      lib/convenience/thumb.js
  8. 107
      lib/drawing.js
  9. 176
      lib/getters.js

@ -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;

@ -3,274 +3,305 @@
// -- args
module.exports = function(proto){
module.exports = function (proto) {
// http://www.graphicsmagick.org/GraphicsMagick.html#details-resize
proto.resize = function(w, h){
return this.arg(["-size", w +"x"+ h], ["-resize ", w +"x"+ h])
proto.resize = function resize (w, h) {
return this.arg(["-size", w +"x"+ h], ["-resize ", w +"x"+ h]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-scale
proto.scale = function(w, h){
return this.arg(null, ["-scale", w +"x"+ h])
proto.scale = function scale (w, h) {
return this.arg(null, ["-scale", w +"x"+ h]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-profile
proto.noProfile = function(){
return this.arg(null, ['+profile "*"'])
proto.noProfile = function noProfile () {
return this.arg(null, ['+profile "*"']);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-resample
proto.resample = function(w, h){
return this.arg(null, ["-resample", w+"x"+h])
proto.resample = function resample (w, h) {
return this.arg(null, ["-resample", w+"x"+h]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-rotate
proto.rotate = function(color, deg){
return this.arg(null, ["-background", color]).arg(null, ["-rotate", deg])
proto.rotate = function rotate (color, deg) {
return this.arg(null, ["-background", color]).arg(null, ["-rotate", deg]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-flip
proto.flip = function(){
return this.arg(null, ["-flip"])
proto.flip = function flip () {
return this.arg(null, ["-flip"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-flop
proto.flop = function(){
return this.arg(null, ["-flop"])
// http://www.graphicsmagick.org/GraphicsMagick.html#details-flop
proto.flop = function flop () {
return this.arg(null, ["-flop"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-crop
proto.crop = function(w, h, x, y){
return this.arg(null, ["-crop", w+"x"+h + "+"+(x||0)+"+"+(y||0)])
proto.crop = function crop (w, h, x, y) {
return this.arg(null, ["-crop", w+"x"+h + "+"+(x||0)+"+"+(y||0)]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-chop
proto.chop = function(w, h, x, y){
return this.arg(["-chop", w+"x"+h + "+"+(x||0)+"+"+(y||0)])
proto.chop = function chop (w, h, x, y) {
return this.arg(["-chop", w+"x"+h + "+"+(x||0)+"+"+(y||0)]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.magnify = function(factor){
return this.arg(["-magnify", factor || 1])
proto.magnify = function magnify (factor) {
return this.arg(["-magnify", factor || 1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.minify = function(factor){
return this.arg(["-minify", factor || 1])
proto.minify = function minify (factor) {
return this.arg(["-minify", factor || 1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-quality
proto.quality = function(val){
return this.arg(["-quality", val || 75])
proto.quality = function quality (val) {
return this.arg(["-quality", val || 75]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-blur
proto.blur = function(radius, sigma){
return this.arg(null, ["-blur", radius + (sigma ? "x"+sigma : "") ])
proto.blur = function blur (radius, sigma) {
return this.arg(null, ["-blur", radius + (sigma ? "x"+sigma : "") ]);
}
// http://www.graphicsmagick.org/convert.html
proto.charcoal = function(factor){
return this.arg(null, ["-charcoal", factor || 2])
proto.charcoal = function charcoal (factor) {
return this.arg(null, ["-charcoal", factor || 2]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-colorize
proto.colorize = function(r, g, b){
return this.arg(null, ["-colorize", [r,g,b].join(",")])
proto.colorize = function colorize (r, g, b) {
return this.arg(null, ["-colorize", [r,g,b].join(",")]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-modulate
proto.modulate = function(b, s, h){
return this.arg(null, ["-modulate", [b,s,h].join(",")])
proto.modulate = function modulate (b, s, h) {
return this.arg(null, ["-modulate", [b,s,h].join(",")]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-antialias
// note: antialiasing is enabled by default
proto.antialias = function(disable){
// note: antialiasing is enabled by default
proto.antialias = function antialias (disable) {
return false === disable
? this.arg(null, ["+antialias"])
: this
: this;
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-depth
proto.bitdepth = function(val){
return this.arg(null, ["-depth", val])
proto.bitdepth = function bitdepth (val) {
return this.arg(null, ["-depth", val]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-colors
proto.colors = function(val){
return this.arg(null, ["-colors", val || 128])
proto.colors = function colors (val) {
return this.arg(null, ["-colors", val || 128]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-colorspace
proto.colorspace = function(val){
proto.colorspace = function colorspace (val) {
return this.arg(null, ["-colorspace", val]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-comment
proto.comment = comment("-comment")
proto.comment = comment("-comment");
// http://www.graphicsmagick.org/GraphicsMagick.html#details-contrast
proto.contrast = function(mult){
var arg = (parseInt(mult) || 0) > 0 ? "+contrast" : "-contrast"
, args = []
mult = Math.abs(mult) || 1
while (mult--) args.push(arg)
return this.arg(null, args)
proto.contrast = function contrast (mult) {
var args = []
, arg = (parseInt(mult, 10) || 0) > 0
? "+contrast"
: "-contrast";
mult = Math.abs(mult) || 1;
while (mult--) {
args.push(arg);
}
return this.arg(null, args);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-cycle
proto.cycle = function(amount){
return this.arg(null, ["-cycle", amount || 2])
proto.cycle = function cycle (amount) {
return this.arg(null, ["-cycle", amount || 2]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.despeckle = function(){
return this.arg(null, ["-despeckle"])
proto.despeckle = function despeckle () {
return this.arg(null, ["-despeckle"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-dither
// note: either colors() or monochrome() must be used for this
// to take effect.
proto.dither = function(on){
return this.arg(null, [(false === on ? "+" : "-") + "dither"])
proto.dither = function dither (on) {
var sign = false === on
? "+"
: "-";
return this.arg(null, [sign + "dither"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.monochrome = function(){
return this.arg(null, ["-monochrome"])
proto.monochrome = function monochrome () {
return this.arg(null, ["-monochrome"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.edge = function(radius){
return this.arg(null, ["-edge", radius || 1])
proto.edge = function edge (radius) {
return this.arg(null, ["-edge", radius || 1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.emboss = function(radius){
return this.arg(null, ["-emboss", radius || 1])
proto.emboss = function emboss (radius) {
return this.arg(null, ["-emboss", radius || 1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.enhance = function(){
return this.arg(null, ["-enhance"])
proto.enhance = function enhance () {
return this.arg(null, ["-enhance"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.equalize = function(){
return this.arg(null, ["-equalize"])
proto.equalize = function equalize () {
return this.arg(null, ["-equalize"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-gamma
proto.gamma = function(r, g, b){
return this.arg(null, ["-gamma", [r,g,b].join(",")])
proto.gamma = function gamma (r, g, b) {
return this.arg(null, ["-gamma", [r,g,b].join()]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.implode = function(factor){
return this.arg(null, ["-implode", factor || 1])
proto.implode = function implode (factor) {
return this.arg(null, ["-implode", factor || 1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-comment
proto.label = comment("-label")
proto.label = comment("-label");
var limits = [ "disk", "file", "map", "memory", "pixels", "threads"];
// http://www.graphicsmagick.org/GraphicsMagick.html#details-limit
proto.limit = function(type, val){
type = type.toLowerCase()
if (-1 == [ "disk"
, "file"
, "map"
, "memory"
, "pixels"
, "threads"
].indexOf(type))
return this
return this.arg(null, ["-limit", type, val])
proto.limit = function limit (type, val) {
type = type.toLowerCase();
if (!~limits.indexOf(type)) {
return this;
}
return this.arg(null, ["-limit", type, val]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.median = function(radius){
return this.arg(null, ["-median", radius || 1])
proto.median = function median (radius) {
return this.arg(null, ["-median", radius || 1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-negate
proto.negative = function(grayscale){
return this.arg(null, [ (grayscale ? "+" : "-") + "negate" ])
proto.negative = function negative (grayscale) {
var sign = grayscale ? "+" : "-";
return this.arg(null, [sign + "negate"]);
}
var noises = [
"uniform"
, "gaussian"
, "multiplicative"
, "impulse"
, "laplacian"
, "poisson" ];
// http://www.graphicsmagick.org/GraphicsMagick.html#details-noise
proto.noise = function(radius){
radius = (String(radius)).toLowerCase()
if (-1 == [ "uniform"
, "gaussian"
, "multiplicative"
, "impulse"
, "laplacian"
, "poisson"
].indexOf(radius))
return this.arg(null, ["-noise", radius])
return this.arg(null, ["+noise", radius])
proto.noise = function noise (radius) {
radius = (String(radius)).toLowerCase();
var sign = ~noises.indexOf(radius)
? "+"
: "-";
return this.arg(null, [sign + "noise", radius]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-paint
proto.paint = function(radius){
return this.arg(null, ["-paint", radius])
proto.paint = function paint (radius) {
return this.arg(null, ["-paint", radius]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-raise
proto.raise = function(w, h){
return this.arg(null, ["-raise", (w||0)+"x"+(h||0)])
proto.raise = function raise (w, h) {
return this.arg(null, ["-raise", (w||0)+"x"+(h||0)]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-raise
proto.lower = function(w, h){
return this.arg(null, ["+raise", (w||0)+"x"+(h||0)])
proto.lower = function lower (w, h) {
return this.arg(null, ["+raise", (w||0)+"x"+(h||0)]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-region
proto.region = function(w, h, x, y){
return this.arg(null, ["-region", (w||0)+"x"+(h||0) + "+"+(x||0)+"+"+(y||0)])
proto.region = function region (w, h, x, y) {
w = w || 0;
h = h || 0;
x = x || 0;
y = y || 0;
return this.arg(null, ["-region", w + "x" + h + "+" + x + "+" + y]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-roll
proto.roll = function(x, y){
x = ((x = parseInt(x) || 0) > 0 ? "+" : "") + x
y = ((y = parseInt(y) || 0) > 0 ? "+" : "") + y
return this.arg(null, ["-roll", x+y])
proto.roll = function roll (x, y) {
x = ((x = parseInt(x, 10) || 0) > 0 ? "+" : "") + x;
y = ((y = parseInt(y, 10) || 0) > 0 ? "+" : "") + y;
return this.arg(null, ["-roll", x+y]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-sharpen
proto.sharpen = function(radius, sigma){
return this.arg(null, ["-sharpen", radius + (sigma ? "x"+sigma : "") ])
proto.sharpen = function sharpen (radius, sigma) {
sigma = sigma
? "x" + sigma
: "";
return this.arg(null, ["-sharpen", radius + sigma]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-solarize
proto.solarize = function(factor){
return this.arg(null, ["-solarize", (factor || 1)+"%"])
proto.solarize = function solarize (factor) {
return this.arg(null, ["-solarize", (factor || 1)+"%"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-spread
proto.spread = function(amount){
return this.arg(null, ["-spread", amount || 5])
proto.spread = function spread (amount) {
return this.arg(null, ["-spread", amount || 5]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-swirl
proto.swirl = function(degrees){
return this.arg(null, ["-swirl", degrees || 180])
proto.swirl = function swirl (degrees) {
return this.arg(null, ["-swirl", degrees || 180]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-type
proto.type = function(type){
return this.arg(["-type", type])
proto.type = function type (type) {
return this.arg(["-type", type]);
}
}
};
/**
* Generates a handler for comments/labels.
*/
function comment (arg) {
return function (format) {
format = String(format);
format = "@" == format.charAt(0)
? format.substring(1)
: format;
function comment(arg){
return function(format){
format = String(format)
format = "@" == format.charAt(0) ? format.substring(1) : format
return this.arg(null, [arg, '"' + format + '"'])
return this.arg(null, [arg, '"' + format + '"']);
}
}

@ -3,41 +3,55 @@
// -- commandline
var exec = require('child_process').exec
var exec = require('child_process').exec;
module.exports = function(proto){
module.exports = function (proto) {
proto.arg = function(inargs, outargs){
if (inargs)
this._in = this._in.concat(inargs)
if (outargs)
this._out = this._out.concat(outargs)
return this
proto.arg = function arg (inargs, outargs) {
if (inargs) {
this._in = this._in.concat(inargs);
}
if (outargs) {
this._out = this._out.concat(outargs);
}
return this;
}
proto.write = function(name, callback){
if (!callback) callback = name, name = null
if ("function" != typeof callback)
proto.write = function write (name, callback) {
if (!callback) callback = name, name = null;
if ("function" !== typeof callback) {
throw new TypeError("gm().write() expects a callback function")
if (!name)
}
if (!name) {
throw new TypeError("gm().write() expects a filename when writing new files")
this.outname = name
return this._exec(this.cmd(), callback)
}
this.outname = name;
return this._exec(this.cmd(), callback);
}
proto._exec = function(cmd, callback) {
var self = this
exec(cmd, function(err, stdout, stderr){
callback.call(self, err, stdout, stderr, cmd)
})
return self
proto._exec = function _exec (cmd, callback) {
var self = this;
exec(cmd, function (err, stdout, stderr) {
callback.call(self, err, stdout, stderr, cmd);
});
return self;
}
proto.cmd = function(){
return "gm convert "
+ this._in.join(" ") + " "
+ this.source + " "
+ this._out.join(" ")+ " "
+ this.outname || this.source
proto.cmd = function cmd () {
return "gm convert "
+ this._in.join(" ")
+ " "
+ this.source
+ " "
+ this._out.join(" ")
+ " "
+ this.outname || this.source;
}
}

@ -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;
}}

@ -1,105 +1,105 @@
module.exports = function(proto){
module.exports = function (proto) {
// http://www.graphicsmagick.org/GraphicsMagick.html#details-fill
proto.fill = function(color){
return this.arg(
null
, [ "-fill"
, "'" + (color || "none") + "'"
]
);
proto.fill = function fill (color) {
return this.arg(null, ["-fill", "'" + (color || "none") + "'"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-stroke
proto.stroke = function(color, width){
if (width)
this.strokeWidth(width)
return this.arg(
null
, [ "-stroke"
, "'" + (color || "none") + "'"
]
);
proto.stroke = function stroke (color, width) {
if (width) {
this.strokeWidth(width);
}
return this.arg(null, ["-stroke", "'" + (color || "none") + "'"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-strokewidth
proto.strokeWidth = function(width){
proto.strokeWidth = function strokeWidth (width) {
return this.arg(null, ["-strokewidth", width]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-font
proto.font = function(font, size){
if (size)
proto.font = function font (font, size) {
if (size) {
this.fontSize(size);
}
return this.arg(null, ["-font", font]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html
proto.fontSize = function(size){
proto.fontSize = function fontSize (size) {
return this.arg(null, ["-pointsize", size]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.draw = function(args){
proto.draw = function draw (args) {
return this.arg(null, ["-draw", "'"+ args.join(" ") + "'"]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawPoint = function(x, y){
proto.drawPoint = function drawPoint (x, y) {
return this.draw(["point", x +","+ y]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawLine = function(x0, y0, x1, y1){
proto.drawLine = function drawLine (x0, y0, x1, y1) {
return this.draw(["line", x0+","+y0, x1+","+y1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawRectangle = function(x0, y0, x1, y1, wc, hc){
proto.drawRectangle = function drawRectangle (x0, y0, x1, y1, wc, hc) {
var shape = "rectangle"
, lastarg
if ("undefined" != typeof wc){
, lastarg;
if ("undefined" !== typeof wc) {
shape = "roundRectangle";
if ("undefined" == typeof hc) hc = wc;
if ("undefined" === typeof hc) {
hc = wc;
}
lastarg = wc+","+hc;
}
return this.draw([shape, x0+","+y0, x1+","+y1, lastarg]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawArc = function(x0, y0, x1, y1, a0, a1){
proto.drawArc = function drawArc (x0, y0, x1, y1, a0, a1) {
return this.draw(["arc", x0+","+y0, x1+","+y1, a0+","+a1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawEllipse = function(x0, y0, rx, ry, a0, a1){
proto.drawEllipse = function drawEllipse (x0, y0, rx, ry, a0, a1) {
if (a0 == undefined) a0 = 0;
if (a1 == undefined) a1 = 360;
return this.draw(["ellipse", x0+","+y0, rx+","+ry, a0+","+a1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawCircle = function(x0, y0, x1, y1){
proto.drawCircle = function drawCircle (x0, y0, x1, y1) {
return this.draw(["circle", x0+","+y0, x1+","+y1]);
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawPolyline = function(){
proto.drawPolyline = function drawPolyline () {
return this.draw(["polyline"].concat(formatPoints(arguments)));
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawPolygon = function(){
proto.drawPolygon = function drawPolygon () {
return this.draw(["polygon"].concat(formatPoints(arguments)));
}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawBezier = function(){
proto.drawBezier = function drawBezier () {
return this.draw(["bezier"].concat(formatPoints(arguments)));
}
proto._gravities =
[ "northwest"
proto._gravities = [
"northwest"
, "north"
, "northeast"
, "west"
@ -107,40 +107,43 @@ module.exports = function(proto){
, "east"
, "southwest"
, "south"
, "southeast"
]
;
, "southeast"];
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.drawText = function(x0, y0, text, gravity){
proto.drawText = function drawText (x0, y0, text, gravity) {
var gravity = String(gravity || "").toLowerCase()
, arg = ["text", x0+","+y0, '"'+text+'"'];
if (~this._gravities.indexOf(gravity))
if (~this._gravities.indexOf(gravity)) {
arg = ["gravity", gravity].concat(arg);
}
return this.draw(arg);
}
proto._drawProps =
[ "color"
, "matte"
]
;
proto._drawProps = ["color", "matte"];
// http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
proto.setDraw = function(prop, x, y, method){
proto.setDraw = function setDraw (prop, x, y, method) {
prop = String(prop || "").toLowerCase();
if (!~this._drawProps.indexOf(prop))
if (!~this._drawProps.indexOf(prop)) {
return this;
}
return this.draw([prop, x+","+y, method]);
}
}
function formatPoints(points){
function formatPoints (points) {
var len = points.length
, result = []
, i = 0
for (; i < len; ++i)
result.push( points[i].join(",") );
, i = 0;
for (; i < len; ++i) {
result.push(points[i].join(","));
}
return result;
}

@ -3,88 +3,118 @@
// -- getters
module.exports = function(proto){
; [ 'size'
, 'format'
, 'depth'
, 'color'
, 'res'
, 'filesize'
].forEach(function(getter){
proto[getter] = function(callback) {
var self = this
if (self.data[getter])
return callback.call(self, null, self.data[getter]), self
self.identify(function(err, stdout, stderr, cmd){
if (err) return callback.call(self, err, stdout, stderr, cmd)
callback.call(self, null, self.data[getter])
})
return self
module.exports = function (proto) {
;['size', 'format', 'depth', 'color', 'res', 'filesize'].forEach(function (getter) {
proto[getter] = function (callback) {
var self = this;
if (self.data[getter]) {
callback.call(self, null, self.data[getter]);
return self;
}
self.identify(function (err, stdout, stderr, cmd) {
if (err) {
return callback.call(self, err, stdout, stderr, cmd);
}
callback.call(self, null, self.data[getter]);
});
return self;
}
)
proto.identify = function(callback){
var self = this
if (!callback) return self
if (self._identifying) return self._iq.push(callback), self
if (Object.keys(self.data).length)
return callback.call(self, null, self.data), self
self._iq = [callback]
self._identifying = true
var cmd = "gm identify -ping -verbose " + self.source
self._exec(cmd, function(err, stdout, stderr){
if (err) return callback.call(self, err, stdout, stderr, cmd)
stdout = (stdout||"").trim().replace(/\r\n|\r/g, "\n")
});
proto.identify = function identify (callback) {
var self = this;
if (!callback) return self;
if (self._identifying) {
self._iq.push(callback);
return self;
}
if (Object.keys(self.data).length) {
callback.call(self, null, self.data);
return self;
}
self._iq = [callback];
self._identifying = true;
var cmd = "gm identify -ping -verbose " + self.source;
self._exec(cmd, function (err, stdout, stderr) {
if (err) {
return callback.call(self, err, stdout, stderr, cmd);
}
stdout = (stdout||"").trim().replace(/\r\n|\r/g, "\n");
var parts = stdout.split("\n")
, len = parts.length
, rgx = /^( *)(.*)/
, data = self.data
, result
, keyval
, i = 0
, handle =
{ 'Geometry': function(val){
var split = val.split("x")
data.size =
{ width: parseInt(split[0], 10)
, height: parseInt(split[1], 10)
}
}
, 'Format': function(val){
data.format = val.split(" ")[0]
}
, 'Depth': function(val){
data.depth = parseInt(val, 10)
}
, 'Colors': function(val){
data.color = parseInt(val, 10)
}
, 'Resolution': function(val){
data.res = val
}
, 'Filesize': function(val){
data.filesize = val
}
}
for (; i < len; ++i){
if (result = rgx.exec(parts[i])){
if (2 == result[1].length){
var keyval = result[2].split(":")
if (keyval.length > 1)
if (handle[keyval[0]])
handle[keyval[0]](keyval[1].trim())
else
data[keyval[0]] = keyval[1].trim()
}
, i = 0;
var handle = {};
handle.Geometry = function Geometry (val) {
var split = val.split("x");
data.size = {
width: parseInt(split[0], 10)
, height: parseInt(split[1], 10)
}
};
handle.Format = function Format (val) {
data.format = val.split(" ")[0];
};
handle.Depth = function Depth (val) {
data.depth = parseInt(val, 10);
};
handle.Colors = function Colors (val) {
data.color = parseInt(val, 10);
};
handle.Resolution = function Resolution (val) {
data.res = val;
};
handle.Filesize = function Filesize (val) {
data.filesize = val;
};
for (; i < len; ++i) {
result = rgx.exec(parts[i]);
if (!result) continue;
if (2 !== result[1].length) continue;
var keyval = result[2].split(":");
if (keyval.length <= 1) continue;
if (keyval[0] in handle) {
handle[keyval[0]](keyval[1].trim());
} else {
data[keyval[0]] = keyval[1].trim();
}
}
var idx = self._iq.length
while(idx--)
self._iq[idx].call(self, null, self.data)
self._identifying = false
})
return self
var idx = self._iq.length;
while (idx--) {
self._iq[idx].call(self, null, self.data);
}
self._identifying = false;
});
return self;
}}

Loading…
Cancel
Save