-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharp.js
39 lines (34 loc) · 1.19 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
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const target = path.resolve(__dirname, 'src', 'public', 'images', 'heros', 'hero-image-large.jpg');
const destination = path.resolve(__dirname, 'dist', 'images', 'heros');
// Membuat direktori tujuan jika belum ada
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true });
}
// Memeriksa apakah file target ada
if (!fs.existsSync(target)) {
console.error('File target not found:', target);
process.exit(1); // Keluar dari proses dengan kode error
}
// Mengubah ukuran gambar dengan lebar 800px, dengan prefix -large.jpg
sharp(target)
.resize(800)
.toFile(path.resolve(destination, 'hero-image-large.jpg'), (err) => {
if (err) {
console.error('Error resizing image:', err);
} else {
console.log('Image resized and saved successfully');
}
});
// Mengubah ukuran gambar dengan lebar 480px, dengan prefix -small.jpg
sharp(target)
.resize(480)
.toFile(path.resolve(destination, 'hero-image-small.jpg'), (err) => {
if (err) {
console.error('Error resizing image:', err);
} else {
console.log('Image resized and saved successfully');
}
});