-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimage-processing.js
More file actions
44 lines (37 loc) · 926 Bytes
/
image-processing.js
File metadata and controls
44 lines (37 loc) · 926 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
33
34
35
36
37
38
39
40
41
42
43
const sharp = require('sharp');
const fs = require('fs');
const dir = 'src/images/cropped';
const filePrefix = 'pathway-cropped';
const sizes = [
{size : 3000},
{size : 2500},
{size : 2000},
{size : 1000},
{size : 500 }
];
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const pipeline = sharp('src/images/2281710-3k-high.jpg')
.extract({ width: 3000, height: 470, left: 0, top: 780 });
function processImages(img) {
try {
// convert jpegs
pipeline.clone().resize({ width: img.size })
.jpeg({
quality: 80,
})
.timeout({ seconds: 5 })
.toFile(`${dir}/${filePrefix}-${img.size}.jpeg`);
// convert webps
pipeline.clone().resize({ width: img.size })
.webp({
quality: 80,
})
.timeout({ seconds: 5 })
.toFile(`${dir}/${filePrefix}-${img.size}.webp`);
} catch (error) {
console.log(error);
}
}
sizes.map(processImages);