-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
379 lines (311 loc) · 12.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!doctype html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-144368893-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-144368893-1');
</script>
<!-- Google AdSense -->
<script data-ad-client="ca-pub-9470401322137641" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- Custom CSS -->
<link rel="stylesheet" href="css/loader.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Webtoon Viewer</title>
</head>
<body style="padding-top : 70px ;">
<header>
<nav class="navbar navbar-expand-md navbar-light fixed-top bg-light">
<a class="navbar-brand" href="#">Webtoon</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<input id="file-input" type="file" class="form-control-file" multiple data-toggle="tooltip"
data-placement="top" data-delay='{"show":0, "hide":0}'
title="Supported types: zip, jpg, png, gif">
</li>
<li class="nav-item">
Width: <input id="width-input" type="text"> px
</li>
</ul>
<form class="form-inline">
<button class="btn btn-outline-success" type="button" id="btn-top">Top</button>
<button class="btn btn-outline-success" type="button" id="btn-bottom">Bottom</button>
<button class="btn btn-outline-success" type="button" id="btn-merge">Merge</button>
</form>
</div>
</nav>
</header>
<div class="loading" id="loading">Loading…</div>
<div id="images" align="center"></div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script type="text/javascript" src="lib/zip.js"></script>
<script type="text/javascript" src="lib/zip-ext.js"></script>
<script type="text/javascript" src="lib/z-worker.js"></script>
</body>
</html>
<script type="text/javascript">
zip.workerScriptsPath = "lib/";
$(document).ready(function () {
toggleLogger(false);
});
function toggleLogger(show) {
document.getElementById('loading').hidden = show ? false : true;
document.getElementById('file-input').disabled = show;
document.getElementById('width-input').disabled = show;
document.getElementById('btn-top').disabled = show;
document.getElementById('btn-bottom').disabled = show;
document.getElementById('btn-merge').disabled = show;
}
function clearImages() {
var images = document.getElementById('images');
while (images.hasChildNodes()) {
images.removeChild(images.firstChild);
}
}
function fileInputChanged() {
if (this.files.length === 1 && /\.zip$/i.test(this.files[0].name)) {
model.getEntries(this.files[0], function (entries) {
displayImagesFromZip(entries)
});
} else {
for (var i = 0; i < this.files.length; i++) {
if (/\.zip$/i.test(this.files[i].name)) {
return alert("You can choose only one zip file.");
} else if (!/\.(jpe?g|png|gif)$/i.test(this.files[i].name)) {
return alert(files[i].name + " is not an image");
}
}
displayImages(this.files);
}
}
function displayImagesFromZip(entries) {
clearImages();
var images = document.getElementById('images');
var widthInput = document.getElementById("width-input");
entries.sort(function(a,b) {
var nameA = a.filename.toUpperCase();
var nameB = b.filename.toUpperCase();
if (nameA < nameB) {
return -1;
} else if (nameA > nameB) {
return 1;
} else {
return 0;
}
});
entries.forEach(function (entry) {
displayImageFromZip(images, widthInput.value, entry)
});
}
function displayImageFromZip(images, width, entry) {
if (!/\.(jpe?g|png|gif)$/i.test(entry.filename)) {
return; // skip non-image files
}
prepareImageElement(images, entry.filename);
model.getEntryFile(entry, function (blobURL) {
putImage(entry.filename, blobURL, width)
});
}
function displayImages(files) {
clearImages();
var images = document.getElementById('images');
var widthInput = document.getElementById("width-input");
for (var i = 0; i < files.length; i++) {
displayImage(images, widthInput.value, files[i])
}
}
function displayImage(images, width, file) {
prepareImageElement(images, file.name);
var reader = new FileReader();
reader.addEventListener("load", function () {
putImage(file.name, this.result, width);
}, false);
reader.readAsDataURL(file);
}
function prepareImageElement(parent, fileName) {
var div = document.createElement("div");
div.setAttribute("id", fileName);
parent.appendChild(div);
}
function putImage(name, blobURL, width) {
var div = document.getElementById(name);
var image = new Image();
image.title = name;
image.src = blobURL;
if (width) {
image.width = width;
}
div.appendChild(image);
}
function resizeWidths(event) {
if (event.keyCode === 13) { // Enter key pressed
var widthInput = document.getElementById("width-input")
var images = document.getElementById("images");
var children = images.children;
for (var i = 0; i < children.length; i++) {
var image = children[i].firstChild
if (widthInput.value) {
image.width = widthInput.value
} else {
var newImage = new Image();
newImage.title = image.title;
newImage.src = image.src;
children[i].replaceChild(newImage, image);
}
}
}
}
function onerror(message) {
alert(message);
}
var model = (function () {
var URL = window.webkitURL || window.mozURL || window.URL;
return {
getEntries: function (file, onend) {
zip.createReader(new zip.BlobReader(file), function (zipReader) {
zipReader.getEntries(onend);
}, onerror);
},
getEntryFile: function (entry, onend, onprogress) {
var writer, zipFileEntry;
function getData() {
entry.getData(writer, function (blob) {
var blobURL = URL.createObjectURL(blob);
onend(blobURL);
}, onprogress);
}
writer = new zip.BlobWriter();
getData();
}
};
})();
function mergeImagesDisplayed() {
var images = document.getElementById('images');
var children = images.children;
var maxWidth = 0;
var totalHeight = 0;
for (var i = 0; i < children.length; i++) {
var image = children[i].firstChild;
if (maxWidth < image.width) {
maxWidth = image.width;
}
totalHeight += image.height;
}
toggleLogger(true);
// async/await
// https://medium.com/javascript-in-plain-english/async-await-javascript-5038668ec6eb
mergeImagesDisplayedInLocal(maxWidth, totalHeight, children).then(blob => {
if (blob) {
console.log(blob)
toggleLogger(false);
window.open(URL.createObjectURL(blob));
} else {
let ret = confirm("그림이 커서 웹브라우저를 통해 merge 할 수 없습니다. 서버로 전송하여 merge 해야합니다. 진행하시겠습니까? 좀 오래걸릴 수 있습니다.")
if (ret) {
// logger will be disabled asynchronously after the server responds
mergeImagesDisplayedInRemote(maxWidth, totalHeight, children);
} else {
toggleLogger(false);
}
}
});
}
async function mergeImagesDisplayedInLocal(width, height, elements) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
var y = 0;
for (var i = 0; i < elements.length; i++) {
var image = elements[i].firstChild;
const x = (canvas.width - image.width) / 2
ctx.drawImage(image, x, y, image.width, image.height);
y += image.height;
}
let blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/png', 1));
return blob;
}
// http://christopher5106.github.io/web/2015/12/13/HTML5-file-image-upload-and-resizing-javascript-with-progress-bar.html
async function mergeImagesDisplayedInRemote(width, height, elements) {
var formData = new FormData();
formData.append("width", width);
formData.append("height", height);
formData.append("resize", 0);
for (var i = 0; i < elements.length; i++) {
var image = elements[i].firstChild;
let blob = await imageToBlob(image);
console.log(blob);
formData.append("files", blob);
}
var xhr = new XMLHttpRequest();
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
xhr.responseType = "blob";
xhr.onload = function (event) {
toggleLogger(false);
window.open(URL.createObjectURL(xhr.response));
}
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
console.log("upload progress: " + evt.loaded + "/" + evt.total);
}
}, false);
xhr.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
console.log("download progress: " + evt.loaded + "/" + evt.total);
}
}, false);
xhr.open("POST", "https://webtoon-service-2svnippdua-an.a.run.app/v1/merge");
xhr.send(formData);
}
// https://javascript.info/blob#image-to-blob
async function imageToBlob(image) {
let canvas = document.createElement('canvas');
canvas.width = image.width
canvas.height = image.height
let ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, image.width, image.height);
let blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/png', 1));
if (!blob) {
alert(`단일 그림 최대 크기 초과.
Size: ${canvas.width}x${canvas.height}
Please check the max size of each web browser.
https://github.com/jhildenbiddle/canvas-size#desktop`);
}
return blob;
}
document.querySelector('#file-input').addEventListener("change", fileInputChanged, false);
document.querySelector('#width-input').addEventListener("keyup", resizeWidths, false);
document.querySelector('#btn-top').addEventListener("click", function () {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}, false);
document.querySelector('#btn-bottom').addEventListener("click", function () {
document.body.scrollTop = document.body.scrollHeight; // For Safari
document.documentElement.scrollTop = document.documentElement.scrollHeight; // For Chrome, Firefox, IE and Opera
}, false);
document.querySelector('#btn-merge').addEventListener("click", mergeImagesDisplayed, false);
</script>