-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.js
73 lines (60 loc) · 1.87 KB
/
compress.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
65
66
67
68
69
70
71
72
73
import fs from 'fs'
import sharp from 'sharp'
import path from 'path'
import ttf2woff2 from 'ttf2woff2'
const inputDir = './src/assets/inputDir'
const outputDir = "./src/assets/outputDIr";
const targetWidth = 300; // Desired width in pixels
const targetHeight = 300; // Desired height in pixels
const ttfInputDir = './src/assets/Fonts'
const woffOutputDir = './src/assets/woffOutput'
// fs.readdir(ttfInputDir, (err, files) => {
// if (err) {
// console.error("Error reading input directory:", err);
// return;
// }
// files.forEach((file) => {
// const fontPath = path.join(ttfInputDir, file);
// fs.readFile(fontPath, (err, fontBuffer) => {
// if (err) {
// console.error("Error reading font file:", err);
// return;
// }
// const woff2Buffer = ttf2woff2(fontBuffer);
// const outputFile = path.join(
// woffOutputDir,
// file.replace(".ttf", ".woff2")
// );
// fs.writeFile(outputFile, woff2Buffer, (err) => {
// if (err) {
// console.error("Error writing compressed font file:", err);
// return;
// }
// console.log(`Successfully compressed ${file}`);
// });
// });
// });
// });
fs.readdir(inputDir, (err, files) => {
if (err) {
console.error("Error reading input directory:", err);
return;
}
files.forEach((file) => {
const inputFile = path.join(inputDir, file);
const outputFile = path.join(outputDir, file.replace(/\.[^.]+$/, ".webp"));
sharp(inputFile)
.resize(targetWidth, targetHeight, {
fit: 'inside',
withoutEnlargement: true,
})
.webp({ quality: 80 })
.toFile(outputFile, (err) => {
if (err) {
console.error("Error compressing photo:", inputFile, err);
} else {
console.log("Compressed photo saved:", outputFile);
}
});
});
});