-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharp.js
64 lines (58 loc) · 1.58 KB
/
sharp.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
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const target = path.resolve(__dirname, 'src/public/images');
const destination = path.resolve(__dirname, 'dist/images');
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination);
}
fs.readdirSync(target).forEach((image) => {
// mengubah ukuran gambar dengan lebar 800px dan kualitas 50% dengan prefix -large.jpg
sharp(`${target}/${image}`)
.jpeg({
quality: 50,
})
.resize(800)
.toFile(
path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-large.jpg`,
),
);
// mengubah ukuran gambar dengan lebar 800px dan kualitas 50% dengan prefix -large.webp
sharp(`${target}/${image}`)
.webp({
quality: 50,
})
.resize(800)
.toFile(
path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-large.webp`,
),
);
// mengubah ukuran gambar dengan lebar 480px dan kualitas 50% dengan prefix -small.jpg
sharp(`${target}/${image}`)
.jpeg({
quality: 50,
})
.resize(480)
.toFile(
path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-small.jpg`,
),
);
// mengubah ukuran gambar dengan lebar 480px dan kualitas 50% dengan prefix -small.webp
sharp(`${target}/${image}`)
.webp({
quality: 50,
})
.resize(480)
.toFile(
path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-small.webp`,
),
);
});