index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Module dependencies.
  3. */
  4. var Stream = require('stream').Stream;
  5. var EventEmitter = require('events').EventEmitter;
  6. var util = require('util');
  7. util.inherits(gm, EventEmitter);
  8. /**
  9. * Constructor.
  10. *
  11. * @param {String|Number} path - path to img source or ReadableStream or width of img to create
  12. * @param {Number} [height] - optional filename of ReadableStream or height of img to create
  13. * @param {String} [color] - optional hex background color of created img
  14. */
  15. function gm (source, height, color) {
  16. var width;
  17. if (!(this instanceof gm)) {
  18. return new gm(source, height, color);
  19. }
  20. EventEmitter.call(this);
  21. this._options = {};
  22. this.options(this.__proto__._options);
  23. this.data = {};
  24. this._in = [];
  25. this._out = [];
  26. this._outputFormat = null;
  27. this._subCommand = 'convert';
  28. if (source instanceof Stream) {
  29. this.sourceStream = source;
  30. source = height || 'unknown.jpg';
  31. } else if (Buffer.isBuffer(source)) {
  32. this.sourceBuffer = source;
  33. source = height || 'unknown.jpg';
  34. } else if (height) {
  35. // new images
  36. width = source;
  37. source = "";
  38. this.in("-size", width + "x" + height);
  39. if (color) {
  40. this.in("xc:"+ color);
  41. }
  42. }
  43. if (typeof source === "string") {
  44. // then source is a path
  45. // parse out gif frame brackets from filename
  46. // since stream doesn't use source path
  47. // eg. "filename.gif[0]"
  48. var frames = source.match(/(\[.+\])$/);
  49. if (frames) {
  50. this.sourceFrames = source.substr(frames.index, frames[0].length);
  51. source = source.substr(0, frames.index);
  52. }
  53. }
  54. this.source = source;
  55. this.addSrcFormatter(function (src) {
  56. // must be first source formatter
  57. var inputFromStdin = this.sourceStream || this.sourceBuffer;
  58. var ret = inputFromStdin ? '-' : this.source;
  59. if (ret && this.sourceFrames) ret += this.sourceFrames;
  60. src.length = 0;
  61. src[0] = ret;
  62. });
  63. }
  64. /**
  65. * Subclasses the gm constructor with custom options.
  66. *
  67. * @param {options} options
  68. * @return {gm} the subclasses gm constructor
  69. */
  70. var parent = gm;
  71. gm.subClass = function subClass (options) {
  72. function gm (source, height, color) {
  73. if (!(this instanceof parent)) {
  74. return new gm(source, height, color);
  75. }
  76. parent.call(this, source, height, color);
  77. }
  78. gm.prototype.__proto__ = parent.prototype;
  79. gm.prototype._options = {};
  80. gm.prototype.options(options);
  81. return gm;
  82. }
  83. /**
  84. * Augment the prototype.
  85. */
  86. require("./lib/options")(gm.prototype);
  87. require("./lib/getters")(gm);
  88. require("./lib/args")(gm.prototype);
  89. require("./lib/drawing")(gm.prototype);
  90. require("./lib/convenience")(gm.prototype);
  91. require("./lib/command")(gm.prototype);
  92. require("./lib/compare")(gm.prototype);
  93. /**
  94. * Expose.
  95. */
  96. module.exports = exports = gm;
  97. module.exports.utils = require('./lib/utils');
  98. module.exports.compare = require('./lib/compare')();
  99. module.exports.version = JSON.parse(
  100. require('fs').readFileSync(__dirname + '/package.json', 'utf8')
  101. ).version;