index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
  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. cover: function cover(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. }
  22. if (alignBits && typeof alignBits === 'function' && typeof cb === 'undefined') {
  23. cb = alignBits;
  24. alignBits = null;
  25. mode = null;
  26. } else if (typeof mode === 'function' && typeof cb === 'undefined') {
  27. cb = mode;
  28. mode = null;
  29. }
  30. alignBits = alignBits || this.constructor.HORIZONTAL_ALIGN_CENTER | this.constructor.VERTICAL_ALIGN_MIDDLE;
  31. var hbits = alignBits & (1 << 3) - 1;
  32. var vbits = alignBits >> 3; // check if more flags than one is in the bit sets
  33. if (!(hbits !== 0 && !(hbits & hbits - 1) || vbits !== 0 && !(vbits & vbits - 1))) return _utils.throwError.call(this, 'only use one flag per alignment direction', cb);
  34. var alignH = hbits >> 1; // 0, 1, 2
  35. var alignV = vbits >> 1; // 0, 1, 2
  36. var f = w / h > this.bitmap.width / this.bitmap.height ? w / this.bitmap.width : h / this.bitmap.height;
  37. this.scale(f, mode);
  38. this.crop((this.bitmap.width - w) / 2 * alignH, (this.bitmap.height - h) / 2 * alignV, w, h);
  39. if ((0, _utils.isNodePattern)(cb)) {
  40. cb.call(this, null, this);
  41. }
  42. return this;
  43. }
  44. };
  45. };
  46. exports["default"] = _default;
  47. //# sourceMappingURL=index.js.map