forked from Coderior/lovingmemory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (70 loc) · 2.35 KB
/
index.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
const glob = require('glob')
const Jimp = require('jimp')
const fs = require('fs')
const dir = './assets/gallery'
const thumbdir = './assets/thumbs'
const imagewidth = 1800
// export default Vanta;
// if (typeof window !== 'undefined' && window.Vue) {
// window.Vue.component('mc-vanta', Vanta);
// }
if (fs.existsSync(dir)) {
fs.rmdirSync(dir, { recursive: true });
}
if (fs.existsSync(thumbdir)) {
fs.rmdirSync(thumbdir, { recursive: true });
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
if (!fs.existsSync(thumbdir)) {
fs.mkdirSync(thumbdir);
}
const opt = {
nocase: true
}
glob("assets/images/*.+(png|jpg|jpeg|svg)", opt, async function (err, images) {
//console.log(images)
const galleryImages = []
for (const file of images) {
//files.forEach(async function(file) {
let filenameold = file.replace('assets/images/', '')
let filename = filenameold.toLowerCase().replace(/[^\w.]+/g, "_")
//galleryImages.push('assets/gallery/' + filename)
Jimp.read(file)
.then(jfile => {
if (jfile.bitmap.width > imagewidth) {
console.log('resizing ' + filename)
return jfile
.resize(imagewidth, Jimp.AUTO) // resize
.quality(60) // set JPEG quality
.write(dir + '/' + filename); // save
} else {
fs.copyFile(file, dir + '/' + filename, (err) => {
if (err) throw err;
console.log(filename + ' copied to gallery (smaller than ' + imagewidth + ')');
});
}
})
.catch(err => {
console.error(err);
});
// Create thumbnails
Jimp.read(file)
.then(jfile => {
width = 120
height = Jimp.AUTO
if (jfile.bitmap.height < jfile.bitmap.width) {
width = Jimp.AUTO
height = 120
}
return jfile
.resize(width, height, Jimp.RESIZE_BEZIER) // resize
.quality(60) // set JPEG quality
.write(thumbdir + '/' + filename); // save
})
.catch(err => {
console.error(err);
});
}
})