-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassImageHandler.js
48 lines (42 loc) · 1.42 KB
/
classImageHandler.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
const fs = require('fs');
const jimp = require('jimp');
const imageFolder = require('path').dirname(require.main.filename) + `\\images\\`;
const { v4: uuidv4} = require('uuid');
class ImageHandler {
imagePath = '';
imageBuffer = '';
uniqueIdentifier = '';
constructor(imagePath, uniqueIdentifier = '') {
this.imagePath = imagePath;
this.uniqueIdentifier = uniqueIdentifier === '' ? uuidv4() : uniqueIdentifier;
console.log(imageFolder);
return this.createImageBuffer();
}
async createImageBuffer(){
this.imageBuffer = await fs.readFileSync(this.imagePath);
return this;
}
async saveImageToFolder(){
await jimp.read(this.imageBuffer)
.then((pic, err) => {
console.log(err, pic);
pic.resize(400, pic.getHeight())
.quality(80)
.write(`./images/${this.uniqueIdentifier}.jpeg`);
});
// .resize(400)
// .jpeg({ quality: 80})
// .toFile(`${imageFolder}${this.uniqueIdentifier}.jpeg`);
}
static deleteImage(imageName){
return new Promise((resolve, reject) => {
fs.unlink(`${imageFolder}${imageName}`, (err) => {
if(err){
reject(err);
} else
resolve("File deleted.");
});
});
}
}
module.exports = ImageHandler;