{% extends 'base.html.twig' %}
{% block title %}
ToBase64.dev - Convert Image to base64 - Css, Javascript and Data URI
{% endblock %}
{% block h1 %}Convert your Image to Base64{% endblock %}
{% block body %}
<div class="row" style="height: 60%">
<div class="padding col-5 d-flex flex-column">
<div class="row" style="height:85%">
<div class="col-md-12" style="height:100%">
<img id="imageb64" style="
border-left-width: 5px;
border-top-width: 5px;
border-right-width: 5px;
border-bottom-width: 5px;
border-color: #17a2b8;
border-style: solid;
height: 100%;
width: 100%;
" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=">
</div>
</div>
<div class="custom-file" style="margin-top:auto">
<input type="file" class="custom-file-input" id="files">
<label class="custom-file-label" for="files">Choose file</label>
</div>
<!--<input class="form-control" type="file" id="files" name="file" style="width:100%;height: 100%;">-->
</div>
<div class="col-2 d-flex flex-column">
<div class="padding row" style="margin-top:auto;margin-bottom:auto;">
<button type="button" onclick="document.getElementById('output').value=imageB64" class="col-12 btn btn-info"><span class="spinner-border spinner-border-sm" id="loading" role="status" aria-hidden="true" style="display:none"></span><i class="fas fa-long-arrow-alt-right"></i></button>
</div>
</div>
<div class="padding col-5 d-flex flex-column">
<div style="height: 33%">
<p style="margin-bottom: 0; text-align: left;">Base64:</p>
<div style="height: 60%">
<textarea class="form-control" id="output1" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
</div>
<button id="download" type="button" class="btn btn-info" onclick="copyToClipboard('output1')" style="margin-top:auto;width:100%">Copy to clipboard!</button>
</div>
<div style="height: 33%">
<p style="margin-top:1em; margin-bottom: 0; text-align: left;">Base64 for use in <img> elements:</p>
<div style="height: 60%">
<textarea class="form-control" id="output2" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
</div>
<button id="download" type="button" class="btn btn-info" onclick="copyToClipboard('output2')" style="margin-top:auto;width: 100%;">Copy to clipboard!</button>
</div>
<div style="height: 33%">
<p style="margin-top:1em; margin-bottom: 0; text-align: left;">Base64 for use as CSS background:</p>
<div style="height: 60%">
<textarea class="form-control" id="output3" name="output" placeholder="Output will be displayed here..." spellcheck=" false" style="width: 100%;height: 100%;"></textarea>
</div>
<button id="download" type="button" class="btn btn-info mt-1" onclick="copyToClipboard('output3')" style="margin-top:auto;width: 100%;">Copy to clipboard!</button>
</div>
</div>
</div>
<script type="text/javascript">
// Add the following code if you want the name of the file appear on select
$(".custom-file-input").on("change", function() {
var fileName = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
function copyToClipboard(output) {
var copyText = document.getElementById(output);
copyText.select();
document.execCommand("copy");
}
function download() {
var url = document.getElementById('output3').src.replace(/^data:image\/[^;]+/, 'data:image/png');
window.open(url);
}
function toimage() {
var string = document.getElementById('input3').value;
var image = "data:image/png;base64," + string;
document.getElementById('output3').src = image;
document.getElementById('download').disabled = false;
}
function totext() {
var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function(e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0;
e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
o = this._keyStr.indexOf(e.charAt(f++));
u = this._keyStr.indexOf(e.charAt(f++));
a = this._keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function(e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192);
t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
t += String.fromCharCode(r >> 6 & 63 | 128);
t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r);
n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
n += 2 } else { c2 = e.charCodeAt(n + 1);
c3 = e.charCodeAt(n + 2);
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
n += 3 } } return t } }
// Define the string
var string = document.getElementById('input4').value;
// Encode the String
var decodedString = Base64.decode(string);
document.getElementById('output4').value = decodedString;
}
function tobase64() {
var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function(e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0;
e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
o = this._keyStr.indexOf(e.charAt(f++));
u = this._keyStr.indexOf(e.charAt(f++));
a = this._keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function(e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192);
t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
t += String.fromCharCode(r >> 6 & 63 | 128);
t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r);
n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
n += 2 } else { c2 = e.charCodeAt(n + 1);
c3 = e.charCodeAt(n + 2);
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
n += 3 } } return t } }
// Define the string
var string = document.getElementById('input2').value;
// Encode the String
var encodedString = Base64.encode(string);
document.getElementById('output2').value = encodedString;
}
</script>
<script>
function handleFileSelect(evt) {
document.getElementById("loading").style.display = "";
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
document.getElementById('imageb64').src = e.target.result;
document.getElementById("output3").value = "url('" + e.target.result + "')";
document.getElementById("output2").value = e.target.result;
document.getElementById("output1").value = e.target.result.split(',')[1];
imageB64 = e.target.result;
document.getElementById("loading").style.display = "none";
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
{% endblock %}