index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _utils = require("@jimp/utils");
  7. /**
  8. * Scale the image to the given width and height keeping the aspect ratio. Some parts of the image may be letter boxed.
  9. * @param {number} w the width to resize the image to
  10. * @param {number} h the height to resize the image to
  11. * @param {number} alignBits (optional) A bitmask for horizontal and vertical alignment
  12. * @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
  13. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  14. * @returns {Jimp} this for chaining of methods
  15. */
  16. var _default = function _default() {
  17. return {
  18. contain: function contain(w, h, alignBits, mode, cb) {
  19. if (typeof w !== 'number' || typeof h !== 'number') {
  20. return _utils.throwError.call(this, 'w and h must be numbers', cb);
  21. } // permit any sort of optional parameters combination
  22. if (typeof alignBits === 'string') {
  23. if (typeof mode === 'function' && typeof cb === 'undefined') cb = mode;
  24. mode = alignBits;
  25. alignBits = null;
  26. }
  27. if (typeof alignBits === 'function') {
  28. if (typeof cb === 'undefined') cb = alignBits;
  29. mode = null;
  30. alignBits = null;
  31. }
  32. if (typeof mode === 'function' && typeof cb === 'undefined') {
  33. cb = mode;
  34. mode = null;
  35. }
  36. alignBits = alignBits || this.constructor.HORIZONTAL_ALIGN_CENTER | this.constructor.VERTICAL_ALIGN_MIDDLE;
  37. var hbits = alignBits & (1 << 3) - 1;
  38. var vbits = alignBits >> 3; // check if more flags than one is in the bit sets
  39. if (!(hbits !== 0 && !(hbits & hbits - 1) || vbits !== 0 && !(vbits & vbits - 1))) {
  40. return _utils.throwError.call(this, 'only use one flag per alignment direction', cb);
  41. }
  42. var alignH = hbits >> 1; // 0, 1, 2
  43. var alignV = vbits >> 1; // 0, 1, 2
  44. var f = w / h > this.bitmap.width / this.bitmap.height ? h / this.bitmap.height : w / this.bitmap.width;
  45. var c = this.cloneQuiet().scale(f, mode);
  46. this.resize(w, h, mode);
  47. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
  48. this.bitmap.data.writeUInt32BE(this._background, idx);
  49. });
  50. this.blit(c, (this.bitmap.width - c.bitmap.width) / 2 * alignH, (this.bitmap.height - c.bitmap.height) / 2 * alignV);
  51. if ((0, _utils.isNodePattern)(cb)) {
  52. cb.call(this, null, this);
  53. }
  54. return this;
  55. }
  56. };
  57. };
  58. exports["default"] = _default;
  59. //# sourceMappingURL=index.js.map