-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-helpers.js
30 lines (25 loc) · 841 Bytes
/
image-helpers.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
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
async function resizeIconWithSharpAsync(iconSizePx, iconFilename, destinationIconPath) {
const filename = path.join(destinationIconPath, iconFilename);
// sharp can't have same input and output filename, so load to buffer then
// write to disk after resize is complete
let buffer = await sharp(filename)
.resize(iconSizePx, iconSizePx)
.toBuffer();
fs.writeFileSync(filename, buffer);
}
async function getImageDimensionsWithSharpAsync(dirname, basename) {
const filename = path.join(dirname, basename);
try {
let { width, height } = await sharp(filename).metadata();
return { width, height };
} catch (e) {
return null;
}
}
module.exports = {
resizeIconWithSharpAsync,
getImageDimensionsWithSharpAsync,
};