-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (56 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var deprecate = require('util').deprecate;
exports.crop = function(x, y, r) {
var orient = r.split('!')[1];
var ratio = r.split('!')[0].split(':').sort();
var vertical = y > x;
var rotate = y > x && orient === 'h' || x > y && orient === 'v';
if ((vertical || rotate) && !(vertical && rotate)) {
x = x + y;
y = x - y;
x = x - y;
}
var xʹ = x;
var yʹ = x * (ratio[1] / ratio[0]);
if (yʹ > y || rotate && yʹ > x) {
yʹ = y;
xʹ = y * (ratio[1] / ratio[0]);
if (xʹ > x) {
xʹ = x;
yʹ = x * (ratio[0] / ratio[1]);
}
}
var Δx = Math.floor((x - xʹ) / 2);
var Δy = Math.floor((y - yʹ) / 2);
if ((vertical || rotate) && !(vertical && rotate)) {
return [
Δy, // crop top left x
Δx, // crop top left y
y - Δy * 2, // crop width
x - Δx * 2 // crop height
];
} else {
return [
Δx, // crop top left x
Δy, // crop top left y
x - Δx * 2, // crop width
y - Δy * 2 // crop height
];
}
};
exports.fixed = deprecate(exports.crop, 'aspect.fixed: Use aspect.crop instead');
exports.resize = function(x, y, xMax, yMax) {
if (xMax && yMax) {
// Maximum values of height and width given, aspect ratio preserved.
if (y > x) {
return [Math.round(yMax * x / y), yMax];
} else {
return [xMax, Math.round(xMax * y / x)];
}
} else if (xMax) {
// Width given, height automagically selected to preserve aspect ratio.
return [xMax, Math.round(xMax * y / x)];
} else {
// Height given, width automagically selected to preserve aspect ratio.
return [Math.round(yMax * x / y), yMax];
}
};