-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbannerSharer.htm
264 lines (238 loc) · 8.64 KB
/
bannerSharer.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sharer Generator</title>
<style>
html,body{margin:0;padding:0;background-color:#272727}
canvas{display:none;text-rendering:optimizeLegibility}
div{display:inline-block;width:100%;background-color:silver}
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="screenshot">
<input type="text" id="imgSize" placeholder="Size" value="200">
</div>
<img width="300" height="300" id="finalResult" alt="banner" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH5AodAgAMo/D8kgAAAAtJREFUCNdj+A8EAAn7A/1jJsWPAAAAAElFTkSuQmCC">
<script>
window.addEventListener("load", function()
{
// GETTING ALL THE ELEMENTS IN THE WEB FORM
var canvas = document.getElementById("myCanvas");
// CLEARING ALL THE INPUTTED VALUES (IF ANY)
document.getElementById("screenshot").value = null;
document.getElementById("imgSize").value = "200";
// 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 < 0)
{
// IF NOT, RETURNING WITHOUT EXECUTING ANYTHING ELSE
return;
}
try
{
// CREATING THE FILEREADER
var filereader = new FileReader();
// GETTING THE EXTENSION
var extension = document.getElementById("screenshot").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 image2 = new Image;
image2.onload = function()
{
var finalCanvas;
var finalContext;
try
{
// GETTING THE IMAGE RATIO
var ratio = image2.width/image2.height;
var width = document.getElementById("imgSize").value;
var height = document.getElementById("imgSize").value/ratio;
// CREATING THE CANVAS
finalCanvas = document.createElement("canvas");
finalCanvas.width = 300;
finalCanvas.height = 300;
// GETTING THE CONTEXT
finalContext = finalCanvas.getContext("2d");
// DRAWING A WHITE BACKGROUND
finalContext.fillStyle = "#FFFFFF";
finalContext.fillRect(0, 0, 300, 300);
// SETTING THE IMAGE SCALE
var scale = imageSize * 1 / image2.width;
// GETTING THE RESIZED IMAGE
var imagenResized = downScaleImage(image2, scale);
// DRAWING THE RESIZED IMAGE
finalContext.drawImage(imagenResized, 300 / 2 - width / 2, 300 / 2 - height / 2, width, height);
}
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(image2, 300 / 2 - width / 2, 300 / 2 - height / 2, width, height);
}
// UPDATING THE FINAL IMAGE WIDTH AND HEIGHT
document.getElementById("finalResult").style.width = 300 + "px";
document.getElementById("finalResult").style.height = 300 + "px";
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = finalCanvas.toDataURL("data/png");
};
image2.src = filereader.result;
};
// READING/LOADING THE FILE
filereader.readAsDataURL(document.getElementById("screenshot").files[0]);
}
}
catch(err)
{
}
}
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("screenshot").addEventListener("change",function(e){update()});
document.getElementById("imgSize").addEventListener("input",function(e){update()});
});
</script>
</body>
</html>