-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (108 loc) · 2.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const aspectRatios = [{
aspect: 0.67,
ratio: '2/3'
}, {
aspect: 1,
ratio: '1/1'
}, {
aspect: 1.33,
ratio: '4/3'
}, {
aspect: 1.6,
ratio: '16/10'
}, {
aspect: 1.67,
ratio: '5/3'
}, {
aspect: 1.78,
ratio: '16/9'
}, {
aspect: 2.37,
ratio: '21/9'
}];
/**
* @description Check if value is of type 'string'
* @param val
* @returns {boolean}
*/
const isStr = val => typeof val === 'string';
/**
* @description To int
* @param value
* @returns {number}
*/
const toInt = value => parseInt(value, 10);
/**
* @description To float
* @param value
* @param decimalPlaces
* @returns {string}
*/
const toFloat = (value, decimalPlaces = 2) => parseFloat(value).toFixed(decimalPlaces);
/**
* @description Get aspect
* @param width
* @param height
* @returns {*}
*/
const getAspect = (width, height) => {
const aspect = toFloat(width / height);
return isStr(aspect) ? +aspect : aspect;
};
/**
* @description Get matchin ratio
* @param aspect
* @returns {*}
*/
const getMatchingRatio = aspect => {
let match = {};
let matchOffset = Number.MAX_SAFE_INTEGER;
let isMatchFound = false;
aspectRatios.forEach(aspectRatio => {
if (isMatchFound) {
return false;
}
const isBigger = aspect > aspectRatio.aspect;
const isSmaller = aspect < aspectRatio.aspect;
if (!isBigger && !isSmaller) {
isMatchFound = true;
match = aspectRatio;
}
const offset = isBigger ? aspect - aspectRatio.aspect : aspectRatio.aspect - aspect;
if (offset < matchOffset) {
match = aspectRatio;
matchOffset = offset;
}
});
return match;
};
/**
* @description Get image aspect type
* @param width
* @param height
* @param maxIncrease
* @returns object
*/
const getImageOrientation = (width, height, maxIncrease = 15) => {
const isSquare = width === height;
const isLandscape = !isSquare && width > height;
const isPortrait = !isSquare && !isLandscape;
const widthIncrease = width > height ? toInt(((width - height) * 100) / width) : 0;
const heightIncrease = height > width ? toInt(((height - width) * 100) / height) : 0;
const isSquareLikeLandscape = isLandscape && widthIncrease < maxIncrease;
const isSquareLikePortrait = isPortrait && heightIncrease < maxIncrease;
const aspect = getAspect(width, height);
const { ratio } = getMatchingRatio(aspect);
return {
isLandscape,
isPortrait,
isSquare,
isSquareLikeLandscape,
isSquareLikePortrait,
widthIncrease,
heightIncrease,
aspect,
ratio
};
};
module.exports = getImageOrientation;