forked from joyqi/sfz
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
292 lines (255 loc) · 9.05 KB
/
index.html
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
<!--
* @Date: 2023-02-27 23:09:37
* @LastEditors: Timothy
* @LastEditTime: 2023-02-28 02:00:12
* @Description:
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>离线照片水印打码工具</title>
<style>
#container {
margin: 30px;
}
#graph {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr;
}
h1 {
margin: 0
}
p,
article {
margin: 0 0 20px 0
}
article {
font-size: 14px;
color: #777
}
.align-center-box {
display: flex;
align-items: center;
margin-bottom: 10px;
}
label {
color: blue;
font-size: 17px;
margin-right: 10px;
}
p label {
color: black;
display: inline-block;
width: 50px;
font-size: 15px
}
p {
font-size: 15px;
line-height: 30px
}
input#text {
width: 100%;
box-sizing: border-box;
font-size: 16px;
margin-bottom: 10px
}
input[type=range] {
width: 200px;
height: 18px;
vertical-align: text-bottom
}
canvas {
box-sizing: border-box;
width: 100%;
border: 1px dashed #AAA;
cursor: pointer
}
</style>
</head>
<div id="container">
<h1>离线照片水印打码工具</h1>
<article>安全地为你的图片加水印,无任何网络请求,利用浏览器api实现水印,特别适合各种敏感证件(身份证,驾照,护照等)。<a
href="https://github.com/jzsn2018/offline-photo-watermark">Github地址,求 star</a></article>
<div class="align-center-box">
<label for="image">第一步:先选择本地图片(可多选)</label>
<input type="file" id="image" autocomplete="off" multiple>
</div>
<div class="align-center-box">
<label for="text">第二步:输入需要打水印的文字</label>
<input style="flex:1;margin-bottom:0" type="text" id="text" placeholder="请输入文字" autocomplete="off">
</div>
<p class="align-center-box">
<label for="color">颜色</label>
<input style="flex:1;margin-right:30px" type="color" id="color" pattern="#[0-9A-Fa-f]{6}" autocomplete="off"
value="#ffffff"><br>
<label for="alpha">透明度</label>
<input style="flex:1;margin-right:30px" type="range" id="alpha" min="0" max="1" step="0.05" autocomplete="off"
value="0.3"><br>
<label for="space">间隔</label>
<input style="flex:1;margin-right:30px" type="range" id="space" min="1" max="8" step="0.2" autocomplete="off"
value="1.5"><br>
<label for="size">字号</label>
<input style="flex:1;margin-right:30px" type="range" id="size" min="0.5" max="3" step="0.05" autocomplete="off"
value="0.8">
</p>
<label for="text">第三步:点击图片进行单个图片下载</label>
<button onclick="downloadAllPic()">下载全部</button>
<p id="graph"></p>
</div>
<script>
var $, canvas, dataURItoBlob, graph, image, configInputSetting, inputItems,
readFile;
$ = function (sel) {
return document.querySelector(sel);
};
inputItems = ['text', 'color', 'alpha', 'space', 'size'];
configInputSetting = {}; // 保存所有水印
allCanvas = []; // 保存所有的canvas
image = $('#image');
graph = $('#graph');
dataURItoBlob = function (dataURI) {
var arr, binStr, i, len, _i, _ref;
binStr = atob((dataURI.split(','))[1]);
len = binStr.length;
arr = new Uint8Array(len);
for (i = _i = 0, _ref = len - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
arr[i] = binStr.charCodeAt(i);
}
return new Blob([arr], {
type: 'image/png'
});
};
const generateFileName = (fileName) => {
var d, pad;
pad = function (n) {
if (n < 10) {
return '0' + n;
} else {
return n;
}
};
d = new Date;
const timeStr = '' + d.getFullYear() + '-' + (pad(d.getMonth() + 1)) + '-' + (pad(d.getDate())) + ' ' + (
pad(d
.getHours())) + (pad(d.getMinutes())) + (pad(d.getSeconds()))
return fileName + '_' + timeStr + '.png';
};
const redrawCanvas = (canvas, img) => {
// 重新设置canvas的宽高,会清空画布
var w = canvas.width;
var h = canvas.height;
canvas.width = w;
canvas.height = h;
canvas.getContext('2d').drawImage(img, 0, 0);
};
const downloadCanvasAsImage = (canvas, fileName) => {
var blob, imageData, link;
link = document.createElement('a');
link.download = generateFileName(fileName);
imageData = canvas.toDataURL('image/png');
blob = dataURItoBlob(imageData);
link.href = URL.createObjectURL(blob);
graph.appendChild(link);
return setTimeout(function () {
link.click();
return graph.removeChild(link);
}, 10);
}
const downloadAllPic = async () => {
allCanvas.forEach(({
canvas,
img,
fileName
}) => {
downloadCanvasAsImage(canvas, fileName);
})
}
readFile = function (file) {
var fileReader;
if (file == null) {
return;
}
fileReader = new FileReader;
fileReader.onload = function () {
var img;
img = new Image;
img.onload = function () {
var ctx;
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
allCanvas.push({
img: img,
canvas: canvas,
fileName: file.name
});
drawText(canvas, img);
graph.appendChild(canvas);
return canvas.addEventListener('click', () => downloadCanvasAsImage(canvas, file.name));
};
return img.src = fileReader.result;
};
return fileReader.readAsDataURL(file);
};
const makeStyle = () => {
var match;
match = configInputSetting.color.value.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);
return 'rgba(' + (parseInt(match[1], 16)) + ',' + (parseInt(match[2], 16)) + ',' + (parseInt(match[3],
16)) + ',' + configInputSetting.alpha.value + ')';
};
const drawText = (canvas, img) => {
redrawCanvas(canvas, img);
var i, j, margin, step, textSize, width, x, y, _i, _j, textCtx;
if (canvas == null) {
return;
}
textSize = configInputSetting.size.value * Math.max(15, (Math.min(canvas.width, canvas.height)) / 25);
textCtx = canvas.getContext('2d');
//!TODO 倾斜角度
textCtx.rotate(45 * Math.PI / 180);
textCtx.fillStyle = makeStyle();
textCtx.font = 'bold ' + textSize +
'px -apple-system,"Helvetica Neue",Helvetica,Arial,"PingFang SC","Hiragino Sans GB","WenQuanYi Micro Hei",sans-serif';
width = (textCtx.measureText(configInputSetting.text.value)).width;
step = Math.sqrt((Math.pow(canvas.width, 2)) + (Math.pow(canvas.height, 2)));
margin = (textCtx.measureText('啊')).width;
x = Math.ceil(step / (width + margin));
y = Math.ceil((step / (configInputSetting.space.value * textSize)) / 2);
for (i = _i = 0; 0 <= x ? _i <= x : _i >= x; i = 0 <= x ? ++_i : --_i) {
for (j = _j = -y; - y <= y ? _j <= y : _j >= y; j = -y <= y ? ++_j : --_j) {
textCtx.fillText(configInputSetting.text.value, (width + margin) * i, configInputSetting.space
.value * textSize * j);
}
}
};
image.addEventListener('change', function () {
// 处理批量图片
const batchFileHandler = function (file) {
const type = file.type;
if (!['image/png', 'image/jpeg', 'image/gif'].includes(type)) {
return alert('仅支持 png, jpg, gif 图片格式');
}
readFile(file);
};
[...this.files].forEach(batchFileHandler);
});
inputItems.forEach(function (item) {
var el;
el = $('#' + item);
configInputSetting[item] = el;
return el.addEventListener('input', () => {
allCanvas.forEach(({
canvas,
img
}) => {
drawText(canvas, img);
});
});
});
</script>
</html>