-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresizer.js
More file actions
32 lines (26 loc) · 802 Bytes
/
Copy pathresizer.js
File metadata and controls
32 lines (26 loc) · 802 Bytes
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
'use strict'
const sharp = require('sharp');
const FORMAT_OVERRIDE = {
jpg: 'jpeg'
};
const SIZE_PRESETS = {
thumbnail: "200x200",
preview: "500x400",
featured: "1024x768",
banner: "3000x500"
}
function parseSize(sizeString) {
const sz = (SIZE_PRESETS[sizeString])
? SIZE_PRESETS[sizeString].split('x').map((px) => +px)
: sizeString.split('x').map((px) => +px);
return {
w: sz[0] ? sz[0] : null,
h: sz[1] ? sz[1] : null
}
}
module.exports = function getResizer(sizeString, filename) {
const size = parseSize(sizeString);
const rawFormat = filename.split('.').pop();
const format = FORMAT_OVERRIDE[rawFormat] ? FORMAT_OVERRIDE[rawFormat] : rawFormat;
return sharp().resize(size.w, size.h).normalize(true).toFormat(format);
}