Path: blob/master/node_modules/@jimp/plugin-scale/src/index.js
1126 views
import { isNodePattern, throwError } from '@jimp/utils';12export default () => ({3/**4* Uniformly scales the image by a factor.5* @param {number} f the factor to scale the image by6* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)7* @param {function(Error, Jimp)} cb (optional) a callback for when complete8* @returns {Jimp} this for chaining of methods9*/10scale(f, mode, cb) {11if (typeof f !== 'number') {12return throwError.call(this, 'f must be a number', cb);13}1415if (f < 0) {16return throwError.call(this, 'f must be a positive number', cb);17}1819if (typeof mode === 'function' && typeof cb === 'undefined') {20cb = mode;21mode = null;22}2324const w = this.bitmap.width * f;25const h = this.bitmap.height * f;26this.resize(w, h, mode);2728if (isNodePattern(cb)) {29cb.call(this, null, this);30}3132return this;33},3435/**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 to38* @param {number} h the height to resize the image to39* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)40* @param {function(Error, Jimp)} cb (optional) a callback for when complete41* @returns {Jimp} this for chaining of methods42*/43scaleToFit(w, h, mode, cb) {44if (typeof w !== 'number' || typeof h !== 'number') {45return throwError.call(this, 'w and h must be numbers', cb);46}4748if (typeof mode === 'function' && typeof cb === 'undefined') {49cb = mode;50mode = null;51}5253const f =54w / h > this.bitmap.width / this.bitmap.height55? h / this.bitmap.height56: w / this.bitmap.width;57this.scale(f, mode);5859if (isNodePattern(cb)) {60cb.call(this, null, this);61}6263return this;64}65});666768