-
Notifications
You must be signed in to change notification settings - Fork 13
/
how-to-show-base64-images.html
94 lines (66 loc) · 1.59 KB
/
how-to-show-base64-images.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
<html>
<head>
<title>Base64 Example</title>
<style>
body {
font-family: sans-serif;
background-color: #eee;
color: #666;
text-align: center;
padding: 40px;
}
a {
text-decoration: none;
color: orange;
transition: .3s;
display: block;
}
a:hover {
opacity: 0.5;
}
#img img {
max-width: 100px;
display: block;
margin: 10px auto;
}
#css div {
width: 100px;
height: 100px;
background-color: #ddd;
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
margin: 10px auto;
}
</style>
</head>
<body>
<h2>IMG tag</h2>
<div id="img"></div>
<br />
<h2>CSS background-image</h2>
<div id="css"></div>
<br />
<p>* data from
<em>
<a
href="https://orsifrancesco.github.io/instagram-without-api/instagram-cache.json">instagram-cache.json</a>
</em>
</p>
<script>
fetch('instagram-cache.json').then(function (response) {
return response.json();
}).then(function (data) {
console.log(data);
const imgTag = [...data].map(el => `<a target="_blank" href="${el.link}"><img src="data:image/jpg;base64, ${el.image}" /></a>`).join('');
console.log({ imgTag });
document.querySelector('#img').innerHTML = imgTag;
const cssBG = [...data].map(el => `<a target="_blank" href="${el.link}"><div style="background-image: url('data:image/jpg;base64, ${el.image}');"></div></a>`).join('');
console.log({ cssBG });
document.querySelector('#css').innerHTML = cssBG;
}).catch(function (err) {
console.warn('Something went wrong.', err);
});
</script>
</body>
</html>