Path: blob/master/node_modules/@jimp/plugin-resize/src/index.js
1126 views
import { throwError, isNodePattern } from '@jimp/utils';12import Resize from './modules/resize';3import Resize2 from './modules/resize2';45export default () => ({6constants: {7RESIZE_NEAREST_NEIGHBOR: 'nearestNeighbor',8RESIZE_BILINEAR: 'bilinearInterpolation',9RESIZE_BICUBIC: 'bicubicInterpolation',10RESIZE_HERMITE: 'hermiteInterpolation',11RESIZE_BEZIER: 'bezierInterpolation'12},1314class: {15/**16* Resizes the image to a set width and height using a 2-pass bilinear algorithm17* @param {number} w the width to resize the image to (or Jimp.AUTO)18* @param {number} h the height to resize the image to (or Jimp.AUTO)19* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)20* @param {function(Error, Jimp)} cb (optional) a callback for when complete21* @returns {Jimp} this for chaining of methods22*/23resize(w, h, mode, cb) {24if (typeof w !== 'number' || typeof h !== 'number') {25return throwError.call(this, 'w and h must be numbers', cb);26}2728if (typeof mode === 'function' && typeof cb === 'undefined') {29cb = mode;30mode = null;31}3233if (w === this.constructor.AUTO && h === this.constructor.AUTO) {34return throwError.call(this, 'w and h cannot both be set to auto', cb);35}3637if (w === this.constructor.AUTO) {38w = this.bitmap.width * (h / this.bitmap.height);39}4041if (h === this.constructor.AUTO) {42h = this.bitmap.height * (w / this.bitmap.width);43}4445if (w < 0 || h < 0) {46return throwError.call(this, 'w and h must be positive numbers', cb);47}4849// round inputs50w = Math.round(w);51h = Math.round(h);5253if (typeof Resize2[mode] === 'function') {54const dst = {55data: Buffer.alloc(w * h * 4),56width: w,57height: h58};59Resize2[mode](this.bitmap, dst);60this.bitmap = dst;61} else {62const image = this;63const resize = new Resize(64this.bitmap.width,65this.bitmap.height,66w,67h,68true,69true,70buffer => {71image.bitmap.data = Buffer.from(buffer);72image.bitmap.width = w;73image.bitmap.height = h;74}75);76resize.resize(this.bitmap.data);77}7879if (isNodePattern(cb)) {80cb.call(this, null, this);81}8283return this;84}85}86});878889