Path: blob/master/node_modules/@jimp/plugin-contain/es/index.js
1129 views
"use strict";12Object.defineProperty(exports, "__esModule", {3value: true4});5exports["default"] = void 0;67var _utils = require("@jimp/utils");89/**10* Scale the image to the given width and height keeping the aspect ratio. Some parts of the image may be letter boxed.11* @param {number} w the width to resize the image to12* @param {number} h the height to resize the image to13* @param {number} alignBits (optional) A bitmask for horizontal and vertical alignment14* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)15* @param {function(Error, Jimp)} cb (optional) a callback for when complete16* @returns {Jimp} this for chaining of methods17*/18var _default = function _default() {19return {20contain: function contain(w, h, alignBits, mode, cb) {21if (typeof w !== 'number' || typeof h !== 'number') {22return _utils.throwError.call(this, 'w and h must be numbers', cb);23} // permit any sort of optional parameters combination242526if (typeof alignBits === 'string') {27if (typeof mode === 'function' && typeof cb === 'undefined') cb = mode;28mode = alignBits;29alignBits = null;30}3132if (typeof alignBits === 'function') {33if (typeof cb === 'undefined') cb = alignBits;34mode = null;35alignBits = null;36}3738if (typeof mode === 'function' && typeof cb === 'undefined') {39cb = mode;40mode = null;41}4243alignBits = alignBits || this.constructor.HORIZONTAL_ALIGN_CENTER | this.constructor.VERTICAL_ALIGN_MIDDLE;44var hbits = alignBits & (1 << 3) - 1;45var vbits = alignBits >> 3; // check if more flags than one is in the bit sets4647if (!(hbits !== 0 && !(hbits & hbits - 1) || vbits !== 0 && !(vbits & vbits - 1))) {48return _utils.throwError.call(this, 'only use one flag per alignment direction', cb);49}5051var alignH = hbits >> 1; // 0, 1, 25253var alignV = vbits >> 1; // 0, 1, 25455var f = w / h > this.bitmap.width / this.bitmap.height ? h / this.bitmap.height : w / this.bitmap.width;56var c = this.cloneQuiet().scale(f, mode);57this.resize(w, h, mode);58this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {59this.bitmap.data.writeUInt32BE(this._background, idx);60});61this.blit(c, (this.bitmap.width - c.bitmap.width) / 2 * alignH, (this.bitmap.height - c.bitmap.height) / 2 * alignV);6263if ((0, _utils.isNodePattern)(cb)) {64cb.call(this, null, this);65}6667return this;68}69};70};7172exports["default"] = _default;73//# sourceMappingURL=index.js.map7475