-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
135 lines (123 loc) · 4.3 KB
/
gulpfile.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const gulp = require('gulp');
const imagemin = require('imagemin');
const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
const gifsicle = require('gifsicle');
const del = require('del');
const Jimp = require('jimp');
const path = require('path');
const fs = require('fs');
const { execFile } = require('child_process');
const myMkdirSync = function (dir) {
if (fs.existsSync(dir)) {
return;
}
try {
fs.mkdirSync(dir);
} catch (err) {
if (err.code == 'ENOENT') {
myMkdirSync(path.dirname(dir)); //create parent dir
myMkdirSync(dir); //create dir
}
}
};
const compress = async (src, dest, min = 0.7, max = 0.8) => {
try {
await imagemin([src], {
destination: dest,
plugins: [
mozjpeg({ quality: 75 }),
pngquant({
quality: [min, max],
}),
],
});
} catch (e) {
if (e.exitCode === 99) {
console.log(Date.now(), 'Reprocessing image with JIMP instead', src);
//JimpCompress(filepath, altDest);
}
}
};
const resizeAndCompress = async (dir, outputdir, width) => {
const directoryPath = path.join(__dirname, dir);
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
if (!file.includes('.gif')) {
const interOutputPath = `temp/${file}`;
Jimp.read(`${dir}/${file}`, (err, image) => {
try {
if (err) {
throw err;
}
image
.resize(Math.min(width, image.bitmap.width), Jimp.AUTO)
.writeAsync(interOutputPath)
.catch(err => {
console.log('err', err);
})
.then(async () => {
await compress(interOutputPath, outputdir).catch(err =>
console.log('compression-err', err)
);
});
} catch (err) {
console.log('err', err);
}
});
} else {
/**
const stream = spawn(gifsicle, ['--resize-fit-width', width, `${dir}/${file}`]);
myMkdirSync('temp');
stream.stdout.pipe(fs.createWriteStream(`temp/${file}`));
myMkdirSync(outputdir);
const stream2 = spawn(gifsicle, ['--optimize', '2', `temp/${file}`]);
stream2.stdout.pipe(fs.createWriteStream(`${outputdir}/${file}`));
**/
myMkdirSync(outputdir);
execFile(
gifsicle,
['-o3', '--lossy=80', '-o', `${outputdir}/${file}`, `${dir}/${file}`],
err => {
//console.timeEnd('execute time');
if (err) {
throw err;
}
}
);
//stream.stdout.pipe(fs.createWriteStream(`${outputdir}/${file}`));
}
});
});
};
gulp.task('image-resizeAndCompress', async done => {
await Promise.all([
resizeAndCompress('public/images/conf', 'public/processed-images/conf/440', 440),
resizeAndCompress('public/images/work', 'public/processed-images/work/480', 480),
]);
done();
});
gulp.task('image-resizeAndCompress-mozilla', async done => {
await resizeAndCompress(
'public/images/work/mozilla',
'public/processed-images/work/mozilla',
630
),
done();
});
gulp.task('clean:processed-images', () => {
return del(['public/processed-images/**/*']);
});
gulp.task('clean:temp', () => {
return del(['temp/**/*']);
});
gulp.task(
'default',
gulp.series('clean:processed-images', 'image-resizeAndCompress', 'clean:temp')
);