Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-cover/src/index.js
1129 views
1
import { isNodePattern, throwError } from '@jimp/utils';
2
3
/**
4
* Scale the image so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
5
* @param {number} w the width to resize the image to
6
* @param {number} h the height to resize the image to
7
* @param {number} alignBits (optional) A bitmask for horizontal and vertical alignment
8
* @param {string} mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
9
* @param {function(Error, Jimp)} cb (optional) a callback for when complete
10
* @returns {Jimp} this for chaining of methods
11
*/
12
export default () => ({
13
cover(w, h, alignBits, mode, cb) {
14
if (typeof w !== 'number' || typeof h !== 'number') {
15
return throwError.call(this, 'w and h must be numbers', cb);
16
}
17
18
if (
19
alignBits &&
20
typeof alignBits === 'function' &&
21
typeof cb === 'undefined'
22
) {
23
cb = alignBits;
24
alignBits = null;
25
mode = null;
26
} else if (typeof mode === 'function' && typeof cb === 'undefined') {
27
cb = mode;
28
mode = null;
29
}
30
31
alignBits =
32
alignBits ||
33
this.constructor.HORIZONTAL_ALIGN_CENTER |
34
this.constructor.VERTICAL_ALIGN_MIDDLE;
35
const hbits = alignBits & ((1 << 3) - 1);
36
const vbits = alignBits >> 3;
37
38
// check if more flags than one is in the bit sets
39
if (
40
!(
41
(hbits !== 0 && !(hbits & (hbits - 1))) ||
42
(vbits !== 0 && !(vbits & (vbits - 1)))
43
)
44
)
45
return throwError.call(
46
this,
47
'only use one flag per alignment direction',
48
cb
49
);
50
51
const alignH = hbits >> 1; // 0, 1, 2
52
const alignV = vbits >> 1; // 0, 1, 2
53
54
const f =
55
w / h > this.bitmap.width / this.bitmap.height
56
? w / this.bitmap.width
57
: h / this.bitmap.height;
58
this.scale(f, mode);
59
this.crop(
60
((this.bitmap.width - w) / 2) * alignH,
61
((this.bitmap.height - h) / 2) * alignV,
62
w,
63
h
64
);
65
66
if (isNodePattern(cb)) {
67
cb.call(this, null, this);
68
}
69
70
return this;
71
}
72
});
73
74