-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
95 lines (77 loc) · 2.23 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Convert Image to Data-URL in your browser</title>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="../vendor/simple.css" />
<style>
/* Custom Style rules */
#preview {
background-position: center center;
background-repeat: no-repeat;
/* background-size: 100% auto; */
aspect-ratio: 16 / 9;
border: 1px solid currentColor;
}
</style>
<script>
function convertToBase64(event) {
const file = event.target.elements.file.files[0];
const reader = new FileReader();
const output = document.getElementById('output');
const preview = document.getElementById('preview');
output.value = '';
preview.style = '';
reader.onload = (event) => {
const imageBase64 = event.target.result;
output.value = imageBase64;
preview.style.backgroundImage = 'url("' + imageBase64 + '")';
};
reader.readAsDataURL(file);
}
</script>
</head>
<body>
<h1>
Convert Image to Data-URL
<small>(in your browser)</small>
</h1>
<form action="" onsubmit="convertToBase64(event); return false;">
<input id="file" type="file" />
<button type="submit"><strong>Convert</strong></button>
</form>
<code>
<textarea
id="output"
spellcheck="false"
readonly
rows="8"
cols="80"
></textarea>
</code>
<p>Preview:</p>
<div id="preview"></div>
<p>Use it in HTML as:</p>
<pre><code>
<!-- As a regular image: -->
<img alt="" src="data:...">
<!-- As meta-data in the <head>: -->
<link rel="icon" href="data:...">
</code></pre>
<p>Use it in CSS as:</p>
<pre><code>
#example .element-selector {
background-image: url("data:image/...");
}
</code></pre>
<footer>
<a
href="https://github.com/rodekruis/browser-tools/blob/main/docs/image-to-data-url/index.html"
><img
src="https://img.shields.io/badge/GitHub-view%20source-blue?logo=github"
alt="View Source on GitHub"
/></a>
</footer>
</body>
</html>