Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-scale/src/index.js
1126 views
1
import { isNodePattern, throwError } from '@jimp/utils';
2
3
export default () => ({
4
/**
5
* Uniformly scales the image by a factor.
6
* @param {number} f the factor to scale the image by
7
* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
8
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
9
* @returns {Jimp} this for chaining of methods
10
*/
11
scale(f, mode, cb) {
12
if (typeof f !== 'number') {
13
return throwError.call(this, 'f must be a number', cb);
14
}
15
16
if (f < 0) {
17
return throwError.call(this, 'f must be a positive number', cb);
18
}
19
20
if (typeof mode === 'function' && typeof cb === 'undefined') {
21
cb = mode;
22
mode = null;
23
}
24
25
const w = this.bitmap.width * f;
26
const h = this.bitmap.height * f;
27
this.resize(w, h, mode);
28
29
if (isNodePattern(cb)) {
30
cb.call(this, null, this);
31
}
32
33
return this;
34
},
35
36
/**
37
* Scale the image to the largest size that fits inside the rectangle that has the given width and height.
38
* @param {number} w the width to resize the image to
39
* @param {number} h the height to resize the image to
40
* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
41
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
42
* @returns {Jimp} this for chaining of methods
43
*/
44
scaleToFit(w, h, mode, cb) {
45
if (typeof w !== 'number' || typeof h !== 'number') {
46
return throwError.call(this, 'w and h must be numbers', cb);
47
}
48
49
if (typeof mode === 'function' && typeof cb === 'undefined') {
50
cb = mode;
51
mode = null;
52
}
53
54
const f =
55
w / h > this.bitmap.width / this.bitmap.height
56
? h / this.bitmap.height
57
: w / this.bitmap.width;
58
this.scale(f, mode);
59
60
if (isNodePattern(cb)) {
61
cb.call(this, null, this);
62
}
63
64
return this;
65
}
66
});
67
68