-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-creator.js
More file actions
108 lines (95 loc) · 2.7 KB
/
Copy pathimage-creator.js
File metadata and controls
108 lines (95 loc) · 2.7 KB
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
const fs = require('fs');
const path = require('path');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const FFmpeg = require('fluent-ffmpeg');
const sharp = require('sharp');
// eslint-disable-next-line import/no-unresolved
const imagemin = require('imagemin-cjs/cjs');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
const target = path.resolve(__dirname, 'src/public/images');
const destination = path.resolve(__dirname, 'dist/images');
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination);
}
FFmpeg.setFfmpegPath(ffmpegPath);
const gify = (image) => new FFmpeg({
source: path.resolve(__dirname, `${target}/${image}`),
});
const createWebm = (image) => gify(image)
.clone()
.withFps(15)
.toFormat('webm')
.saveToFile(path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}.webm`,
));
const createMp4 = (image) => gify(image)
.clone()
.withVideoCodec('libx264')
.withFps(25)
.toFormat('mp4')
.saveToFile(path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}.mp4`,
));
const createLargeImage = (image) => sharp(`${target}/${image}`)
.resize(800)
.toFile(path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-large.jpg`,
));
const createLargeImageWebp = (image) => sharp(`${target}/${image}`)
.resize(800)
.toFormat('webp')
.toFile(path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-large.webp`,
));
const createSmallImage = (image) => sharp(`${target}/${image}`)
.resize(480)
.toFile(path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-small.jpg`,
));
const createSmallImageWebp = (image) => sharp(`${target}/${image}`)
.resize(480)
.toFormat('webp')
.toFile(path.resolve(
__dirname,
`${destination}/${image.split('.').slice(0, -1).join('.')}-small.webp`,
));
const compressImage = async () => {
await imagemin([`${destination}/*.{jpg,png}`], {
destination,
plugins: [
imageminMozjpeg({
quality: 75,
progressive: true,
}),
imageminPngquant({
quality: [0.4, 0.6],
}),
],
});
};
const createImage = (image) => {
const extention = image.split('.').slice(1).join('.');
switch (extention) {
case 'gif':
createWebm(image);
createMp4(image);
break;
case 'jpg':
createLargeImage(image);
createLargeImageWebp(image);
createSmallImage(image);
createSmallImageWebp(image);
break;
default:
break;
}
};
fs.readdirSync(target)
.forEach((image) => createImage(image));
compressImage();