index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _utils = require("@jimp/utils");
  7. var _default = function _default() {
  8. return {
  9. /**
  10. * Uniformly scales the image by a factor.
  11. * @param {number} f the factor to scale the image by
  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. scale: function scale(f, mode, cb) {
  17. if (typeof f !== 'number') {
  18. return _utils.throwError.call(this, 'f must be a number', cb);
  19. }
  20. if (f < 0) {
  21. return _utils.throwError.call(this, 'f must be a positive number', cb);
  22. }
  23. if (typeof mode === 'function' && typeof cb === 'undefined') {
  24. cb = mode;
  25. mode = null;
  26. }
  27. var w = this.bitmap.width * f;
  28. var h = this.bitmap.height * f;
  29. this.resize(w, h, mode);
  30. if ((0, _utils.isNodePattern)(cb)) {
  31. cb.call(this, null, this);
  32. }
  33. return this;
  34. },
  35. /**
  36. * Scale the image to the largest size that fits inside the rectangle that has the given width and height.
  37. * @param {number} w the width to resize the image to
  38. * @param {number} h the height to resize the image to
  39. * @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
  40. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  41. * @returns {Jimp} this for chaining of methods
  42. */
  43. scaleToFit: function scaleToFit(w, h, mode, cb) {
  44. if (typeof w !== 'number' || typeof h !== 'number') {
  45. return _utils.throwError.call(this, 'w and h must be numbers', cb);
  46. }
  47. if (typeof mode === 'function' && typeof cb === 'undefined') {
  48. cb = mode;
  49. mode = null;
  50. }
  51. var f = w / h > this.bitmap.width / this.bitmap.height ? h / this.bitmap.height : w / this.bitmap.width;
  52. this.scale(f, mode);
  53. if ((0, _utils.isNodePattern)(cb)) {
  54. cb.call(this, null, this);
  55. }
  56. return this;
  57. }
  58. };
  59. };
  60. exports["default"] = _default;
  61. //# sourceMappingURL=index.js.map