-
Notifications
You must be signed in to change notification settings - Fork 3
/
bannerMergerVertical1.htm
355 lines (320 loc) · 11.5 KB
/
bannerMergerVertical1.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Merger Vertical Banner Generator</title>
<style>
html,body{margin:0;padding:0;background-color:white}
canvas{display:none;text-rendering:optimizeLegibility}
div{position:absolute;left:0;right:0;top:0;width:auto}
input[type=text]{height:30px;width:90px;font-family:Arial;font-size:13px;margin:0;padding:0;box-sizing:border-box}
input[type=file]{height:30px;font-family:Arial;font-size:13px;line-height:30px;vertical-align:top;margin:0;padding:0;box-sizing:border-box}
img{display:inline-block}
</style>
</head>
<body>
<div>
<input type="file" id="screenshot1">
<input type="file" id="screenshot2"><br />
<img width="320" height="200" id="finalResult" alt="banner" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH5AodAgAMo/D8kgAAAAtJREFUCNdj+A8EAAn7A/1jJsWPAAAAAElFTkSuQmCC">
</div>
<script>
var finalImage1 = null;
var finalImage2 = null;
window.addEventListener("load", function()
{
// CLEARING ALL THE INPUTTED VALUES (IF ANY)
document.getElementById("screenshot1").value = null;
document.getElementById("screenshot2").value = null;
// USING A VARIABLE IN ORDER TO KNOW WHEN ALL THE GRAPHICAL ELEMENTS ARE LOADED
var elementsLoadedCounter = 0;
function update()
{
// INCREASING THE ELEMENT LOADED COUNTER
elementsLoadedCounter++;
// CHECKING IF THE RIGHT AMOUNT OF ELEMENTS ARE LOADED
if (elementsLoadedCounter < 2)
{
// IF NOT, RETURNING WITHOUT EXECUTING ANYTHING ELSE
return;
}
try
{
// CREATING THE FILEREADER
var filereader = new FileReader();
// GETTING THE EXTENSION
var extension = document.getElementById("screenshot1").files[0].name.split(".").pop().toLowerCase();
// CHECKING THE EXTENSION IN ORDER TO KNOW IF THE FILE IS AN IMAGE
if (extension=="jpg" | extension=="jpeg" | extension=="png")
{
// SETTING WHAT WILL HAPPEN WHEN THE FILE IS LOADED
filereader.onload = function()
{
// LOADING THE IMAGE FILE
var image1 = new Image;
image1.onload = function()
{
// UPDATING THE FINAL IMAGE 1 VALUE
finalImage1 = image1;
// CHECKING IF ALL THE IMAGES ARE LOADED
if (finalImage1!=null && finalImage2!=null)
{
// MERGING THE IMAGES
mergeImages();
}
};
image1.src = filereader.result;
};
// READING/LOADING THE FILE
filereader.readAsDataURL(document.getElementById("screenshot1").files[0]);
}
else
{
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
}
}
catch(err)
{
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
}
try
{
// CREATING THE FILEREADER
var filereader2 = new FileReader();
// GETTING THE EXTENSION
var extension = document.getElementById("screenshot2").files[0].name.split(".").pop().toLowerCase();
// CHECKING THE EXTENSION IN ORDER TO KNOW IF THE FILE IS AN IMAGE
if (extension=="jpg" | extension=="jpeg" | extension=="png")
{
// SETTING WHAT WILL HAPPEN WHEN THE FILE IS LOADED
filereader2.onload = function()
{
// LOADING THE IMAGE FILE
var image2 = new Image;
image2.onload = function()
{
// UPDATING THE FINAL IMAGE 2 VALUE
finalImage2 = image2;
// CHECKING IF ALL THE IMAGES ARE LOADED
if (finalImage1!=null && finalImage2!=null)
{
// MERGING THE IMAGES
mergeImages();
}
};
image2.src = filereader2.result;
};
// READING/LOADING THE FILE
filereader2.readAsDataURL(document.getElementById("screenshot2").files[0]);
}
else
{
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
}
}
catch(err)
{
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
}
}
function mergeImages()
{
// UPDATING THE FINAL IMAGE WIDTH AND HEIGHT
document.getElementById("finalResult").style.width = finalImage1.width + "px";
document.getElementById("finalResult").style.height = (finalImage1.height + finalImage2.height) + "px";
// CREATING THE CANVAS
var finalCanvas = document.createElement("canvas");
// CREATING THE VARIABLES THAT WILL CONTAIN THE SECOND IMAGE RESIZED VALUES (IF NECESSARY)
var finalImage2Width;
var finalImage2Height;
// CHECKING IF THE IMAGES HAVE THE SAME WIDTH
if (finalImage1.width==finalImage2.width)
{
finalCanvas.width = finalImage1.width;
finalCanvas.height = finalImage1.height + finalImage2.height;
}
else
{
// GETTING THE SECOND IMAGE SIZE
finalImage2Width = finalImage1.width;
finalImage2Height = Math.ceil(finalImage2.height * (finalImage1.width/finalImage2.width));
// UPDATING CANVAS SIZE
finalCanvas.width = finalImage1.width;
finalCanvas.height = finalImage1.height + finalImage2Height;
}
// GETTING THE CONTEXT
var finalContext = finalCanvas.getContext("2d");
// DRAWING A WHITE BACKGROUND
finalContext.fillStyle = "#FFFFFF";
finalContext.fillRect(0, 0, finalImage1.width, finalImage1.height + finalImage2.height);
// DRAWING THE FIRST SCREENSHOT
finalContext.drawImage(finalImage1, 0, 0, finalImage1.width, finalImage1.height);
// CHECKING IF THE IMAGES HAVE THE SAME WIDTH
if (finalImage1.width==finalImage2.width)
{
// DRAWING THE SECOND SCREENSHOT
finalContext.drawImage(finalImage2, 0, finalImage1.height, finalImage2.width, finalImage2.height);
}
else
{
try
{
// SETTING THE IMAGE SCALE
var scale = finalImage2Width * 1 / finalImage1.width;
// GETTING THE RESIZED IMAGE
var imagenResized = downScaleImage(finalImage2, scale);
// DRAWING THE RESIZED IMAGE
finalContext.drawImage(imagenResized, 0, finalImage1.height, finalImage2Width, finalImage2Height);
}
catch(err)
{
// IF THERE IS AN ERROR, IT MEANS THAT THE FILE IMAGE IS SMALLER THAN
// THE SIZE THAT IT SUPPOSED TO FILL. SO, BECAUSE OF THIS,
// IN THE FOLLOWING LINE THE IMAGE IS STRETCHED TO FILL.
finalContext.drawImage(finalImage2, 0, finalImage1.height, finalImage2Width, finalImage2Height);
}
// UPDATING THE FINAL IMAGE HEIGHT
document.getElementById("finalResult").style.height = (finalImage1.height + finalImage2Height) + "px";
}
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = finalCanvas.toDataURL("data/png");
}
function downScaleImage(img, scale)
{
var imgCV = document.createElement("canvas");
imgCV.width = img.width;
imgCV.height = img.height;
var imgCtx = imgCV.getContext("2d");
imgCtx.drawImage(img, 0, 0);
return downScaleCanvas(imgCV, scale);
}
function downScaleCanvas(cv, scale)
{
if (!(scale < 1) || !(scale > 0)) throw ("scale must be a positive number <1 ");
var sqScale = scale * scale;
var sw = cv.width;
var sh = cv.height;
var tw = Math.floor(sw * scale);
var th = Math.floor(sh * scale);
var sx = 0, sy = 0, sIndex = 0;
var tx = 0, ty = 0, yIndex = 0, tIndex = 0;
var tX = 0, tY = 0;
var w = 0, nw = 0, wx = 0, nwx = 0, wy = 0, nwy = 0;
var crossX = false;
var crossY = false;
var sBuffer = cv.getContext("2d").getImageData(0, 0, sw, sh).data;
var tBuffer = new Float32Array(4 * sw * sh);
var sR = 0, sG = 0, sB = 0;
var sA = 0;
for (sy = 0; sy < sh; sy++)
{
ty = sy * scale;
tY = 0 | ty;
yIndex = 4 * tY * tw;
crossY = (tY != (0 | ty + scale));
if (crossY)
{
wy = (tY + 1 - ty);
nwy = (ty + scale - tY - 1);
}
for (sx = 0; sx < sw; sx++, sIndex += 4)
{
tx = sx * scale;
tX = 0 | tx;
tIndex = yIndex + tX * 4;
crossX = (tX != (0 | tx + scale));
if (crossX)
{
wx = (tX + 1 - tx);
nwx = (tx + scale - tX - 1);
}
sR = sBuffer[sIndex];
sG = sBuffer[sIndex + 1];
sB = sBuffer[sIndex + 2];
sA = sBuffer[sIndex + 3];
if (!crossX && !crossY)
{
tBuffer[tIndex] += sR * sqScale;
tBuffer[tIndex + 1] += sG * sqScale;
tBuffer[tIndex + 2] += sB * sqScale;
tBuffer[tIndex + 3] += sA * sqScale;
}
else if (crossX && !crossY)
{
w = wx * scale;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
tBuffer[tIndex + 3] += sA * w;
nw = nwx * scale
tBuffer[tIndex + 4] += sR * nw;
tBuffer[tIndex + 5] += sG * nw;
tBuffer[tIndex + 6] += sB * nw;
tBuffer[tIndex + 7] += sA * nw;
}
else if (crossY && !crossX)
{
w = wy * scale;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
tBuffer[tIndex + 3] += sA * w;
nw = nwy * scale
tBuffer[tIndex + 4 * tw ] += sR * nw;
tBuffer[tIndex + 4 * tw + 1] += sG * nw;
tBuffer[tIndex + 4 * tw + 2] += sB * nw;
tBuffer[tIndex + 4 * tw + 3] += sA * nw;
}
else
{
w = wx * wy;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
tBuffer[tIndex + 3] += sA * w;
nw = nwx * wy;
tBuffer[tIndex + 4] += sR * nw;
tBuffer[tIndex + 5] += sG * nw;
tBuffer[tIndex + 6] += sB * nw;
tBuffer[tIndex + 7] += sA * nw;
nw = wx * nwy;
tBuffer[tIndex + 4 * tw] += sR * nw;
tBuffer[tIndex + 4 * tw + 1] += sG * nw;
tBuffer[tIndex + 4 * tw + 2] += sB * nw;
tBuffer[tIndex + 4 * tw + 3] += sA * nw;
nw = nwx * nwy;
tBuffer[tIndex + 4 * tw + 4] += sR * nw;
tBuffer[tIndex + 4 * tw + 5] += sG * nw;
tBuffer[tIndex + 4 * tw + 6] += sB * nw;
tBuffer[tIndex + 4 * tw + 7] += sA * nw;
}
}
}
var resCV = document.createElement("canvas");
resCV.width = tw;
resCV.height = th;
var resCtx = resCV.getContext("2d");
var imgRes = resCtx.getImageData(0, 0, tw, th);
var tByteBuffer = imgRes.data;
var pxIndex = 0;
for (sIndex = 0, tIndex = 0; pxIndex < tw * th; sIndex += 4, tIndex += 4, pxIndex++)
{
tByteBuffer[tIndex] = Math.ceil(tBuffer[sIndex]);
tByteBuffer[tIndex + 1] = Math.ceil(tBuffer[sIndex + 1]);
tByteBuffer[tIndex + 2] = Math.ceil(tBuffer[sIndex + 2]);
tByteBuffer[tIndex + 3] = Math.ceil(tBuffer[sIndex + 3]);
}
resCtx.putImageData(imgRes, 0, 0);
return resCV;
}
// SETTING WHAT WILL HAPPEN WHEN THE USER SELECTS A FILE
document.getElementById("screenshot1").addEventListener("change",function(e){update()});
document.getElementById("screenshot2").addEventListener("change",function(e){update()});
});
</script>
</body>
</html>