jquery.watermark.js
9.82 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* @preserve
* Project: jQuery plugin Watermark
* Description: Add watermark on images use HTML5 and Javascript.
* Author: Zzbaivong (devs.forumvi.com)
* Version: 0.2
* License: MIT
*/
/*
* jquery-boilerplate - v3.4.0
* A jump-start for jQuery plugins development.
* http://jqueryboilerplate.com
*
* Made by Zeno Rocha
* Under MIT License
*/
;(function ($, window, document, undefined) {
'use strict';
var pluginName = 'watermark',
defaults = {
path: 'watermark.png',
dataPath: false,
text: '',
textWidth: 130,
textSize: 13,
textColor: 'white',
textBg: 'rgba(0, 0, 0, 0.4)',
gravity: 'se', // nw | n | ne | w | e | sw | s | se
opacity: 0.7,
margin: 0,
fullOverlay: false,
outputWidth: 'auto',
outputHeight: 'auto',
outputType: 'jpeg', // jpeg | png | webp
done: function (imgURL) {
this.src = imgURL;
},
fail: function ( /*imgURL*/ ) {
// console.error(imgURL, 'image error!');
},
always: function ( /*imgURL*/ ) {
// console.log(imgURL, 'image URL!');
}
};
function Plugin(element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
$.extend(Plugin.prototype, {
init: function () {
var _this = this,
ele = _this.element,
set = _this.settings,
actualPath = set.dataPath ? $(ele).data(set.dataPath) : set.path,
wmData = {
imgurl: actualPath,
type: 'png',
cross: true
},
imageData = {
imgurl: ele.src,
cross: true,
type: set.outputType,
width: set.outputWidth,
height: set.outputHeight
};
// Watermark dạng base64
if (actualPath.search(/data:image\/(png|jpg|jpeg|gif);base64,/) === 0) {
wmData.cross = false;
}
// Ảnh đang duyệt dạng base64
if (ele.src.search(/data:image\/(png|jpg|jpeg|gif);base64,/) === 0) {
imageData.cross = false;
}
var defer = $.Deferred();
$.when(defer).done(function (imgObj) {
imageData.wmObj = imgObj;
_this.imgurltodata(imageData, function (dataURL) {
set.done.call(ele, dataURL);
set.always.call(ele, dataURL);
});
});
if (set.text !== '') {
wmData.imgurl = _this.textwatermark();
wmData.cross = false;
}
_this.imgurltodata(wmData, function (imgObj) {
defer.resolve(imgObj);
});
},
/**
* Chuyển text sang ảnh để làm watermark
* @returns {String} URL ảnh dạng base64
*/
textwatermark: function () {
var _this = this,
set = _this.settings,
canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
//宽度=字体宽度
w = set.textWidth,
//高度=字体大小+8
h = set.textSize + 8;
//定制画布的长宽
canvas.width = 250;
canvas.height = 250;
//设置填充背景
ctx.fillStyle = set.textBg;
ctx.rotate(-15*Math.PI/180);
//ctx.fillRect(0, 150, 100, 50);
ctx.fillStyle = set.textColor;
ctx.textAlign = 'center';
//ctx.font = '500 ' + set.textSize + 'px Sans-serif';
ctx.font = 'bold ' + set.textSize + 'px Microsoft YaHei';
//ctx.font = 'bold ' + set.textSize + 'px 宋体';
ctx.fillText(set.text, 55,w );
/*
if(set.textBg==='black'){
ctx.fillText(set.text, 45,w );
}else{
ctx.fillText(set.text, 27,w );
}*/
//console.log(canvas.toDataURL());
return canvas.toDataURL();
},
/**
* Chuyển ảnh sang dạng base64
* @param {Object} data Các thông số thiết lập để phân biệt loại ảnh và với watermark
* @param {String} callback URL ảnh dạng base64
*/
imgurltodata: function (data, callback) {
var _this = this,
set = _this.settings,
ele = _this.element;
var img = new Image();
if (data.cross) {
img.crossOrigin = 'Anonymous';
}
img.onload = function () {
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var w = this.width, // image height
h = this.height, // image width
ctxH;
if (data.wmObj) {
if (data.width !== 'auto' && data.height === 'auto' && data.width < w) {
h = h / w * data.width;
w = data.width;
} else if (data.width === 'auto' && data.height !== 'auto' && data.height < h) {
w = w / h * data.height;
h = data.height;
} else if (data.width !== 'auto' && data.height !== 'auto' && data.width < w && data.height < h) {
w = data.width;
h = data.height;
}
}
// Xoay dọc watermark sử dụng text, khi ở vị trí giữa mép dọc
if ((set.gravity === 'w' || set.gravity === 'e') && !data.wmObj) {
canvas.width = h;
canvas.height = w;
ctxH = -h;
ctx.rotate(90* Math.PI / 180);
} else {
canvas.width = w;
canvas.height = h;
ctxH = 0;
}
// Tô nền trắng cho ảnh xuất ra dạng jpeg
if (data.type === 'jpeg') {
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, w, h);
}
ctx.drawImage(this, 0, ctxH, w, h);
// Xử lý watermark được chèn vào
if (data.wmObj) {
// Độ trong suốt
var op = set.opacity;
if (op > 0 && op < 1) {
ctx.globalAlpha = set.opacity;
}
// Vị trí chèn, gọi theo hướng trên bản đồ
var wmW = set.fullOverlay ? w : data.wmObj.width,
wmH = set.fullOverlay ? h : data.wmObj.height,
pos = set.margin,
gLeft, gTop;
switch (set.gravity) { // nw | n | ne | w | e | sw | s | se
case 'nw': // Tây bắc
gLeft = pos;
gTop = pos;
break;
case 'n': // Bắc
gLeft = w / 2 - wmW / 2;
gTop = pos;
break;
case 'ne': // Đông Bắc
gLeft = w - wmW - pos;
gTop = pos;
break;
case 'w': // Tây
gLeft = pos;
gTop = h / 2 - wmH / 2;
break;
case 'e': // Đông
gLeft = w - wmW - pos;
gTop = h / 2 - wmH / 2;
break;
case 'sw': // Tây Nam
gLeft = pos;
gTop = h - wmH - pos;
break;
case 's': // Nam
gLeft = w / 2 - wmW / 2;
gTop = h - wmH - pos;
break;
default: // Đông Nam
gLeft = w - wmW - pos;
gTop = h - wmH - pos;
}
ctx.drawImage(data.wmObj, gLeft, gTop, wmW, wmH);
}
// Xuất ra url ảnh dạng base64
var dataURL = canvas.toDataURL('image/' + data.type);
if (typeof callback === 'function') {
if (data.wmObj) { // Đã có watermark
callback(dataURL);
} else { // watermark
var wmNew = new Image();
wmNew.src = dataURL;
callback(wmNew);
}
}
canvas = null;
};
// Xử lý ảnh tải lỗi hoặc có thể do từ chối CORS headers
img.onerror = function () {
set.fail.call(this, this.src);
set.always.call(ele, this.src);
return false;
};
img.src = data.imgurl;
}
});
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
};
}(jQuery, window, document));