forked from yuanguozheng/coderyuan-image-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermarker.js
109 lines (95 loc) · 3.73 KB
/
watermarker.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
const path = require('path');
const fs = require('fs');
const gm = require('gm');
const config = require('./config');
const LogUtil = require('./log');
const WATERMARK_PATH = config.ConfigManager.getInstance().getValue(config.keys.KEY_WATERMARK_PATH);
/**
* An util to attach watermark on target image.
*/
class WaterMarker {
/**
* add watermark & save to a file.
*
* @param {string} rawImagePath original image file path.
* @param {string} outputPath output image file path.
* @param {function(Error)} callback cb.
*/
static markAndSave(rawImagePath, outputPath, callback) {
WaterMarker._getImageSize(rawImagePath, (width, height) => {
if (width <= 0 || height <= 0) {
callback(new Error('Image load error.'))
return;
}
// default watermark rate
const rateH = 0.15;
const rateW = 0.50;
WaterMarker._getImageSize(WATERMARK_PATH, (wmWidth, wmHeight) => {
if (wmWidth <= 0 || wmHeight <= 0) {
callback(new Error('Image load error.'))
return;
}
let targetWmWidth = 0;
let targetWmHeight = 0;
// if width > height, use width as reference.
if (width > height) {
targetWmWidth = width * rateW;
targetWmHeight = targetWmWidth / wmWidth * wmHeight;
} else {
targetWmHeight = height * rateH;
targetWmWidth = targetWmHeight / wmHeight * wmWidth;
}
if (!targetWmHeight || !targetWmWidth) {
callback(new Error('Image size error.'))
return;
}
targetWmHeight = Math.ceil(targetWmHeight);
targetWmWidth = Math.ceil(targetWmWidth);
const targetX = Math.ceil(width - targetWmWidth);
const targetY = Math.ceil(height - targetWmHeight);
const tempDir = config.ConfigManager.getInstance().getImageTempPath();
const fullTargetTempWmPath = path.join(tempDir, `wm_${new Date() * 1}.png`);
/**
* 1. Resize the watermark as a temporary file.
* 2. Do mosaic (use original image and resized watermark).
* 3. Delete temporary watermark.
*/
const resizedWm = gm(WATERMARK_PATH).resize(targetWmWidth, targetWmHeight).write(fullTargetTempWmPath, (err) => {
if (err) {
callback(err);
return;
}
gm(rawImagePath).composite(fullTargetTempWmPath)
.geometry(`+${targetX}+${targetY}`)
.write(outputPath, (err) => {
if (err) {
LogUtil.error(err);
}
if (fs.existsSync(fullTargetTempWmPath)) {
fs.unlinkSync(fullTargetTempWmPath);
}
callback(err);
});
});
});
});
}
/**
* Get image size.
*
* @param {string} path
* @param {function(number, number)} callback
* @private
*/
static _getImageSize(path, callback) {
gm(path).size((err, imageSize) => {
if (err) {
LogUtil.error(err);
callback(-1, -1);
} else {
callback(imageSize.width, imageSize.height);
}
});
}
}
module.exports = WaterMarker;