-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
104 lines (87 loc) · 3.32 KB
/
server.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
const express = require('express');
const multer = require('multer');
const execa = require('execa');
const sharp = require('sharp');
const server = express();
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
function optimize(res, buffer, type) {
const args = [];
if (type == 'image/png') {
args.push('-o', '6');
args.push('-i', '1');
args.push('--strip', 'all');
args.push('--stdout');
args.push('-Z');
args.push('-p');
args.push('-a');
args.push('-');
(async () => {
const { stdout } = await execa('oxipng', args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
});
res.end(stdout);
})();
}
if (type == 'image/jpeg') {
args.push('-quality', '90');
args.push('-optimize');
args.push('-progressive');
args.push('-quant-table', '3');
args.push('-sample', '1x1');
(async () => {
const { stdout } = await execa('cjpeg', args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
});
res.end(stdout);
})();
}
}
server.post('/', upload.fields([{ name: 'image' }, { name: 'watermark' }]), function (req, res) {
let image_file = req.files['image'];
let watermark = req.files['watermark'];
const width = req.body.width ? parseInt(req.body.width) : null;
const height = req.body.height ? parseInt(req.body.height) : null;
sharp(image_file[0].buffer)
.metadata()
.then(imageMeta => {
if (typeof watermark !== 'undefined') {
sharp(watermark[0].buffer)
.resize(Math.round(imageMeta.width / (req.body.factor ? parseInt(req.body.factor) : 10)))
.toBuffer()
.then(dataWatermark => {
sharp(dataWatermark)
.metadata()
.then(watermarkMeta => {
sharp(image_file[0].buffer)
.resize(width, height)
.composite([
{
input: dataWatermark,
top: imageMeta.height - watermarkMeta.height - (req.body.padding ? parseInt(req.body.padding) : 15),
left: imageMeta.width - watermarkMeta.width - (req.body.padding ? parseInt(req.body.padding) : 15)
}
])
.toBuffer()
.then(data => {
optimize(res, data, image_file[0].mimetype);
});
})
});
} else {
sharp(image_file[0].buffer)
.resize(width, height)
.toBuffer()
.then(data => {
optimize(res, data, image_file[0].mimetype);
});
}
});
});
server.listen(8000, () => {
console.log('Server started!')
})