-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
92 lines (77 loc) · 2.93 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
83
84
85
86
87
88
89
90
91
92
/**
* dynamic-watermark
*
*
* Copyright (c) 2017 Navjot Dhanawat
* Licensed under the MIT license.
*/
/**
* Dynamic Watermark is npm watermark module to add watermark over image.
* It can add image as well as text watermark on given positions.
* @param {options}
* @return {callback}
*/
var gm = require('gm');
var embed = function(options, callback) {
var source = options.source; // Source file path
var logo = options.logo; // Logo file path
var ext = source.split('.').pop();
var destination = options.destination; // Destination file path
/**
* Left bottom: X = 10, Y = logoY
* Right bottom: X = logoX, Y = logoY
* Left Top: X = 10, Y = 10
* Right Top: X = logoX, Y = 10
*/
var position = options.position; //'right-bottom';
var type = options.type; //'text';
var text = options.text ? options.text : '';
gm(source)
.size(function(err, size) {
if (!err) {
var logoWidth = (size.width / 10);
var logoHeight = (size.height / 10);
var logoX = size.width - (size.width / 8);
var logoY = size.height - (size.height / 8);
switch (position) {
case 'left-top':
var logoX = 10;
var logoY = 10;
break;
case 'left-bottom':
var logoX = 10;
var logoY = size.height - (size.height / 8);
break;
case 'right-top':
var logoX = size.width - (size.width / 8);
var logoY = 10;
break;
default:
if (typeof position == 'object') {
var logoWidth = position.logoWidth;
var logoHeight = position.logoHeight;
var logoX = position.logoX;
var logoY = position.logoY;
}
break;
}
if (type === 'text') {
var textColor = options.textOption ? options.textOption.color : '#000000';
var fontSize = options.textOption ? options.textOption.fontSize : 20;
gm(source)
.fill(textColor)
.drawText(logoX, logoY, text)
.fontSize(fontSize + 'px')
.write(destination, callback);
} else {
gm(source)
.draw(['image over ' + logoX + ',' + logoY + ' ' + logoWidth + ',' + logoHeight + ' "' + logo + '"'])
.write(destination, callback);
}
} else {
console.error(err);
callback(err);
}
});
};
module.exports.embed = embed;