-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine.js
118 lines (116 loc) · 4.42 KB
/
engine.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const sharp = require('sharp'),
fs = require("fs"),
readChunk = require('read-chunk'),
path = require("path"),
fileType = require('file-type'),
chalk = require("chalk");
class engine {
constructor(CURR_DIR) {
this.CURR_DIR = CURR_DIR;
}
optimize(imgpath, outputPath, imgQuality, isProgressive) {
if(!fs.existsSync(outputPath)){
fs.mkdirSync(outputPath, { recursive: true });
}
fs.readdirSync(imgpath).forEach(file=>{
const filePath = path.join(imgpath, file);
const stats = fs.statSync(filePath);
if (!stats.isDirectory()){
const imageType = this.getFileType(filePath);
if(imageType==='jpg' || imageType==='jpeg'){
sharp(filePath)
.jpeg({
progressive: isProgressive,
quality: imgQuality
})
.toFile(path.join(outputPath, file))
.then(() => {
console.log(
chalk.yellow("Images(.jpg) optimized:for dir:", imgpath)
);
});
}
if(imageType==='png'){
sharp(filePath)
.png({
progressive: isProgressive
})
.toFile(path.join(outputPath, file))
.then(() => {
console.log(
chalk.yellow("Images(.jpg) optimized:for dir:", imgpath)
);
});
}
}
})
}
resize(imgpath, outputPath, height, width, resizeAllImgs){
let withoutEnlargement = resizeAllImgs?false:true;
let minImgPath = path.join(outputPath)
if(!fs.existsSync(outputPath)){
fs.mkdirSync(outputPath, { recursive: true });
}if(!fs.existsSync(minImgPath)){
fs.mkdirSync(minImgPath, { recursive: true });
}
fs.readdirSync(imgpath).forEach(file=>{
const filePath = path.join(imgpath, file);
const stats = fs.statSync(filePath);
if (!stats.isDirectory()){
if((typeof(height)==='string' && height !== 'auto') || (typeof(width)==='string' && width !== 'auto')){
console.log(
chalk.red("Invalid height or width defined hence no image will be resized")
);
return false;
}
else if(height==='auto' && width !== 'auto'){
sharp(filePath)
.resize(width, null, {
withoutEnlargement: withoutEnlargement
})
.toFile(path.join(minImgPath,file))
.then(() => {
console.log(
chalk.yellow("Images resized:for dir:", imgpath)
);
});
}
else if(height!=='auto' && width === 'auto'){
sharp(filePath)
.resize(null, height,{
withoutEnlargement: withoutEnlargement
})
.toFile(path.join(minImgPath,file))
.then(() => {
console.log(
chalk.yellow("Images resized:for dir:", imgpath)
);
});
}
else if(height!=='auto' && width!=='auto'){
sharp(filePath)
.resize(width, height,{
withoutEnlargement: withoutenlargement
})
.toFile(path.join(minImgPath,file))
.then(() => {
console.log(
chalk.yellow("Images resized:for dir:", imgpath)
);
});
}
else{
console.log(
chalk.red("No height or width defined hence no image will be resized")
);
return;
}
}
})
}
getFileType(file){
const buffer = readChunk.sync(file, 0, fileType.minimumBytes);
return fileType(buffer).ext;
}
}
module.exports = engine;