-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresizingImage.js
More file actions
69 lines (50 loc) · 1.65 KB
/
Copy pathresizingImage.js
File metadata and controls
69 lines (50 loc) · 1.65 KB
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
const sharp = require('sharp');
const fs = require('fs');
const sizeOf = require('image-size');
exports.resize = (width, height, from, to, form, size) => {
const executeResize = () => {
for (let i = 0; i < rawFiles.length; i++) {
const fileName = rawFiles[i].split('.')[0];
sharp(`./${from}/${rawFiles[i]}`)
.rotate()
.resize(width, height)
.toFile(`./${to}/${fileName}_${size}.${form}`);
}
};
const rawFiles = fs.readdirSync(`${from}`);
fs.mkdir(`./${to}`, function (err) {
if (err) ;
executeResize();
});
};
exports.resizeByPercent = (percent, from, to, form) => {
let widthList = [];
const array = () => {
for (let i = 0; i < rawFiles.length; i++) {
const imageSize = sizeOf(`./${from}/${rawFiles[i]}`);
widthList.push({
width: imageSize.width,
height: imageSize.height
});
}
};
const executeResize = () => {
for (let i = 0; i < rawFiles.length; i++) {
const fileName = rawFiles[i].split('.')[0];
console.log(fileName);
sharp(`./${from}/${rawFiles[i]}`)
.rotate()
.resize(widthList[i].width * percent, widthList[i].height * percent)
.toFile(`./${to}/${fileName}_medium.${form}`)
.then(function () {
console.log(i);
});
}
};
const rawFiles = fs.readdirSync(`${from}`);
fs.mkdir(`./${to}`, function (err) {
if (err) ;
array();
executeResize();
});
};