-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
486 lines (435 loc) · 22.9 KB
/
index.html
File metadata and controls
486 lines (435 loc) · 22.9 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="light dark">
<title>Online base64 image encoder</title>
<meta name="description"
content="Use the form by selecting any file and the base64 string will be generated and placed below for easy copy/pasting. The files are not uploaded to any server. The encoding happens in the browser on your machine." />
<meta property="og:title" content="Online base64 image encoder" />
<meta property="og:image" content="https://img2base64.swimburger.net/og-image.png" />
<meta name="twitter:title" content="Online base64 image encoder" />
<meta name="twitter:image" content="https://img2base64.swimburger.net/og-image.png" />
<meta property="og:description"
content="Use the form by selecting any file and the base64 string will be generated and placed below for easy copy/pasting. The files are not uploaded to any server. The encoding happens in the browser on your machine." />
<meta name="twitter:description"
content="Use the form by selecting any file and the base64 string will be generated and placed below for easy copy/pasting. The files are not uploaded to any server. The encoding happens in the browser on your machine." />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@RealSwimburger" />
<meta name="twitter:creator" content="@RealSwimburger" />
<link rel="icon" type="image/png" href="https://swimburger.net/content/favicon/favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="https://swimburger.net/content/favicon/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="https://swimburger.net/content/favicon/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/png" href="https://swimburger.net/content/favicon/favicon-128.png" sizes="128x128" />
<link rel="icon" type="image/png" href="https://swimburger.net/content/favicon/favicon-196x196.png"
sizes="196x196" />
<link rel="apple-touch-icon-precomposed" sizes="57x57"
href="https://swimburger.net/content/favicon/apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60"
href="https://swimburger.net/content/favicon/apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72"
href="https://swimburger.net/content/favicon/apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76"
href="https://swimburger.net/content/favicon/apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114"
href="https://swimburger.net/content/favicon/apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120"
href="https://swimburger.net/content/favicon/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144"
href="https://swimburger.net/content/favicon/apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152"
href="https://swimburger.net/content/favicon/apple-touch-icon-152x152.png" />
<script async src="https://www.googletagmanager.com/gtag/js?id=G-DTV3FZ5XWK"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-DTV3FZ5XWK');
</script>
<script>
// Set Bootstrap color theme before CSS loads to prevent flash of wrong theme
document.documentElement.setAttribute(
'data-bs-theme',
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
);
</script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
<style>
/* Brand */
:root {
--brand-darkest: #4a0636;
--brand-dark: #6c0a51;
--brand-mid: #a02c56;
--brand-accent: #ffc966;
--brand-gradient: linear-gradient(135deg, #4a0636, #a02c56);
--bs-body-font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
--bs-primary: #a02c56;
--bs-primary-rgb: 160, 44, 86;
--bs-link-color: #a02c56;
--bs-link-color-rgb: 160, 44, 86;
--bs-link-hover-color: #6c0a51;
}
[data-bs-theme="light"] {
--bs-body-bg: #fafafa;
}
/* Primary button */
.btn-primary {
--bs-btn-bg: var(--brand-mid);
--bs-btn-border-color: var(--brand-mid);
--bs-btn-hover-bg: var(--brand-dark);
--bs-btn-hover-border-color: var(--brand-dark);
--bs-btn-active-bg: var(--brand-darkest);
--bs-btn-active-border-color: var(--brand-darkest);
--bs-btn-focus-shadow-rgb: 160, 44, 86;
}
/* GitHub corner */
.github-corner:hover .octo-arm {
animation: octocat-wave 560ms ease-in-out;
}
@keyframes octocat-wave {
0%, 100% { transform: rotate(0); }
20%, 60% { transform: rotate(-25deg); }
40%, 80% { transform: rotate(10deg); }
}
@media (max-width: 500px) {
.github-corner:hover .octo-arm { animation: none; }
.github-corner .octo-arm { animation: octocat-wave 560ms ease-in-out; }
}
/* Header */
.site-header {
background: var(--brand-gradient);
border: none;
}
.site-header h1,
.site-header p {
color: #fff;
}
.site-header p {
opacity: .85;
}
.site-header .badge {
background-color: rgba(255, 255, 255, 0.18) !important;
color: var(--brand-accent);
}
/* Drop zone */
.drop-zone {
border: 2px dashed var(--bs-border-color);
border-radius: var(--bs-border-radius-lg);
padding: 3rem 2rem;
text-align: center;
cursor: pointer;
transition: background-color 0.2s ease, border-color 0.2s ease;
user-select: none;
}
.drop-zone:hover,
.drop-zone.drag-over {
border-color: var(--brand-mid);
background-color: rgba(160, 44, 86, 0.03);
}
.drop-zone:focus {
outline: 2px solid var(--brand-mid);
outline-offset: 2px;
}
/* Output textarea */
.output-text-wrapper {
position: relative;
}
.output-text-wrapper button {
position: absolute;
top: 8px;
right: 8px;
padding: .35em .6em;
line-height: 1;
z-index: 1;
}
#output-text {
font-family: 'Cascadia Code', ui-monospace, 'JetBrains Mono', 'Fira Code', Consolas, monospace;
font-size: .8rem;
resize: vertical;
}
/* Image preview */
.preview-card {
min-height: 200px;
display: flex;
align-items: center;
justify-content: center;
}
.preview-card img {
max-height: 480px;
object-fit: contain;
}
/* Footer */
footer {
background: var(--brand-gradient);
color: rgba(255, 255, 255, 0.8);
font-size: .875rem;
}
footer a {
color: var(--brand-accent);
text-decoration: underline;
}
footer img {
float: left;
margin-right: 6px;
margin-top: 1px;
opacity: .8;
}
/* Snippet generator */
.snippet-section {
display: none;
}
.snippet-input {
font-family: 'Cascadia Code', ui-monospace, 'JetBrains Mono', 'Fira Code', Consolas, monospace;
font-size: .75rem;
cursor: default;
}
</style>
</head>
<body class="d-flex flex-column min-vh-100">
<a href="https://github.com/Swimburger/ImageToBase64String" class="github-corner"
aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250"
style="fill:#4a0636; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true">
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
<path
d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
<path
d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
fill="currentColor" class="octo-body"></path>
</svg></a>
<header class="site-header py-3 mb-2">
<div class="container">
<h1 class="h5 fw-semibold mb-1">Base64 Image Encoder</h1>
<p class="small mb-0">
Convert any file to base64 — entirely in your browser, nothing uploaded.
</p>
</div>
</header>
<main class="container flex-grow-1 py-5">
<div class="mb-5">
<div id="drop-zone" class="drop-zone" role="button" tabindex="0"
aria-label="Drop file here, click to browse, or paste from clipboard">
<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" fill="currentColor"
class="bi bi-cloud-arrow-up mb-3 text-primary" viewBox="0 0 16 16" aria-hidden="true">
<path fill-rule="evenodd"
d="M7.646 5.146a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 6.707V10.5a.5.5 0 0 1-1 0V6.707L6.354 7.854a.5.5 0 1 1-.708-.708l2-2z"/>
<path
d="M4.406 3.342A5.53 5.53 0 0 1 8 2c2.69 0 4.923 2 5.166 4.579C14.758 6.804 16 8.137 16 9.773 16 11.569 14.502 13 12.687 13H3.781C1.708 13 0 11.366 0 9.318c0-1.763 1.266-3.223 2.942-3.593.143-.863.698-1.723 1.464-2.383zm.653.757c-.757.653-1.153 1.44-1.153 2.056v.448l-.445.049C2.064 6.805 1 7.952 1 9.318 1 10.785 2.23 12 3.781 12h8.906C13.98 12 15 10.988 15 9.773c0-1.216-1.02-2.228-2.313-2.228h-.5v-.5C12.188 4.825 10.328 3 8 3a4.53 4.53 0 0 0-2.941 1.1z"/>
</svg>
<p class="fw-medium mb-1">Drop a file here</p>
<p class="small mb-0">
or <span style="color:var(--brand-mid)" class="fw-medium">click to browse</span>
· paste with <kbd>Ctrl+V</kbd>
</p>
<input id="file-input" type="file" class="visually-hidden" tabindex="-1">
</div>
</div>
<div class="row g-5">
<div class="col-lg-6">
<div class="d-flex justify-content-between align-items-center mb-2">
<label for="output-text" class="form-label mb-0 text-body-secondary">Base64 output</label>
<div class="btn-group btn-group-sm" role="group" aria-label="Output format">
<input type="radio" class="btn-check" name="format" id="format-dataurl" value="dataurl" checked>
<label class="btn btn-outline-secondary py-0 px-2" style="font-size:.75rem;" for="format-dataurl">Data URL</label>
<input type="radio" class="btn-check" name="format" id="format-base64" value="base64">
<label class="btn btn-outline-secondary py-0 px-2" style="font-size:.75rem;" for="format-base64">Base64 only</label>
</div>
</div>
<div class="output-text-wrapper">
<button id="copy-button" type="button" class="btn btn-sm btn-primary" title="Copy to clipboard">
<svg id="clipboard-icon" xmlns="http://www.w3.org/2000/svg" width="14" height="14"
fill="currentColor" class="bi bi-clipboard me-1" viewBox="0 0 16 16">
<path
d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z" />
<path
d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z" />
</svg><span id="copy-label">Copy</span>
</button>
<textarea id="output-text" class="form-control" rows="20" readonly></textarea>
</div>
<div id="snippet-section" class="snippet-section mt-3">
<div class="d-flex flex-column gap-2">
<div class="input-group input-group-sm">
<span class="input-group-text text-body-secondary" style="width:5rem;">HTML</span>
<input id="snippet-html" type="text" class="form-control snippet-input" readonly>
<button class="btn btn-outline-secondary snippet-copy" data-target="snippet-html" type="button">Copy</button>
</div>
<div class="input-group input-group-sm">
<span class="input-group-text text-body-secondary" style="width:5rem;">CSS</span>
<input id="snippet-css" type="text" class="form-control snippet-input" readonly>
<button class="btn btn-outline-secondary snippet-copy" data-target="snippet-css" type="button">Copy</button>
</div>
<div class="input-group input-group-sm">
<span class="input-group-text text-body-secondary" style="width:5rem;">Markdown</span>
<input id="snippet-md" type="text" class="form-control snippet-input" readonly>
<button class="btn btn-outline-secondary snippet-copy" data-target="snippet-md" type="button">Copy</button>
</div>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="preview-card p-3">
<img id="output-image" class="img-fluid rounded" loading="lazy" decoding="async"
src="" alt="" style="display:none;">
<p id="preview-placeholder" class="text-body-secondary small mb-0">Image preview</p>
</div>
</div>
</div>
</main>
<footer class="mt-auto py-3">
<div class="container">
<img src="https://swimburger.net/content/img/burger.svg" width="36" height="20" alt="Swimburger logo">
Tool by Niels Swimberghe —
<a href="https://swimburger.net">swimburger.net</a> ·
<a href="https://twitter.com/RealSwimburger">X (Twitter)</a> ·
<a href="https://privacypolicies.com/privacy/view/95b2095f4609044d9618c21b06f5df75">Privacy Policy</a>
</div>
</footer>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="copy-toast" class="toast align-items-center text-bg-success border-0"
role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body fw-medium">Copied to clipboard!</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto"
data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
crossorigin="anonymous"></script>
<script>
(() => {
const COPY_FEEDBACK_MS = 2000;
const fileInput = document.getElementById('file-input');
const dropZone = document.getElementById('drop-zone');
const outputImage = document.getElementById('output-image');
const previewPlaceholder = document.getElementById('preview-placeholder');
const outputText = document.getElementById('output-text');
const copyButton = document.getElementById('copy-button');
const copyLabel = document.getElementById('copy-label');
const clipboardIcon = document.getElementById('clipboard-icon');
const copyToast = new bootstrap.Toast(document.getElementById('copy-toast'), { delay: COPY_FEEDBACK_MS });
const snippetSection = document.getElementById('snippet-section');
const snippetHtml = document.getElementById('snippet-html');
const snippetCss = document.getElementById('snippet-css');
const snippetMd = document.getElementById('snippet-md');
let lastDataUrl = '';
function getFormat() {
return document.querySelector('input[name="format"]:checked').value;
}
function updateOutput() {
if (!lastDataUrl) return;
outputText.value = getFormat() === 'base64'
? lastDataUrl.split(',')[1]
: lastDataUrl;
}
function processFile(file) {
const reader = new FileReader();
reader.onload = () => {
lastDataUrl = reader.result;
updateOutput();
if (file.type?.startsWith('image/')) {
outputImage.src = lastDataUrl;
outputImage.style.display = 'block';
previewPlaceholder.style.display = 'none';
if (file.size <= 100 * 1024) {
const preview = lastDataUrl.slice(0, 40) + '\u2026';
snippetHtml.value = `<img src="${preview}" alt="">`;
snippetCss.value = `background-image: url("${preview}");`;
snippetMd.value = ``;
snippetSection.style.display = 'block';
} else {
snippetSection.style.display = 'none';
}
} else {
outputImage.src = '';
outputImage.style.display = 'none';
previewPlaceholder.style.display = 'block';
snippetSection.style.display = 'none';
}
};
reader.readAsDataURL(file);
}
// File picker
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) processFile(file);
});
// Drag & drop
dropZone.addEventListener('dragover', (event) => {
event.preventDefault();
dropZone.classList.add('drag-over');
});
dropZone.addEventListener('dragleave', (event) => {
if (!dropZone.contains(event.relatedTarget)) {
dropZone.classList.remove('drag-over');
}
});
dropZone.addEventListener('drop', (event) => {
event.preventDefault();
dropZone.classList.remove('drag-over');
const file = event.dataTransfer.files[0];
if (file) processFile(file);
});
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
fileInput.click();
}
});
// Paste from clipboard
document.addEventListener('paste', (event) => {
const file = [...event.clipboardData.items]
.find(item => item.kind === 'file')
?.getAsFile();
if (file) processFile(file);
});
// Load ramen image as default
fetch('ramen.jpg')
.then(r => r.blob())
.then(blob => processFile(blob))
.catch(() => {});
// Format toggle
document.querySelectorAll('input[name="format"]').forEach(
input => input.addEventListener('change', updateOutput)
);
// Select all on click or keyboard focus
['click', 'focus'].forEach(e =>
outputText.addEventListener(e, () => outputText.setSelectionRange(0, outputText.value.length))
);
// Snippet copy buttons — build full snippet from lastDataUrl at copy time
const snippetBuilders = {
'snippet-html': () => `<img src="${lastDataUrl}" alt="">`,
'snippet-css': () => `background-image: url("${lastDataUrl}");`,
'snippet-md': () => ``,
};
document.querySelectorAll('.snippet-copy').forEach(btn => {
btn.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(snippetBuilders[btn.dataset.target]());
const original = btn.textContent;
btn.textContent = 'Copied!';
setTimeout(() => btn.textContent = original, COPY_FEEDBACK_MS);
} catch {}
});
});
copyButton.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(outputText.value);
copyToast.show();
clipboardIcon.style.display = 'none';
copyLabel.textContent = 'Copied!';
setTimeout(() => {
copyLabel.textContent = 'Copy';
clipboardIcon.style.display = 'inline-block';
}, COPY_FEEDBACK_MS);
} catch {}
});
})();
</script>
</body>
</html>