templates/base64-to-image.html.twig line 1

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}
  3. ToBase64.dev - Convert base64 text to images - Download it
  4. {% endblock %}
  5. {% block h1 %}Convert your base64 to an image{% endblock %}
  6. {% block body %}
  7. <div class="row" style="height: 60%">
  8. <div class="padding col-5">
  9. <form method="post" action="" class="form" style="height: 100%">
  10. <textarea class="form-control" id="input3" name="input3" placeholder="Type (or paste) here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
  11. </form>
  12. </div>
  13. <div class="col-2 d-flex flex-column">
  14. <div class="padding row" style="margin-top:auto;margin-bottom:auto;">
  15. <button type="button" onclick="toimage()" class="col-12 btn btn-info"><i class="fas fa-long-arrow-alt-right"></i></button>
  16. </div>
  17. </div>
  18. <div class="padding col-5 d-flex flex-column">
  19. <div style="height: 85%">
  20. <img id="output3" style="
  21. border-left-width: 5px;
  22. border-top-width: 5px;
  23. border-right-width: 5px;
  24. border-bottom-width: 5px;
  25. border-color: #17a2b8;
  26. border-style: solid;
  27. height: 100%;
  28. width: 100%;
  29. " src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=">
  30. </div>
  31. <button id="download" type="button" class="btn btn-info" onclick="download()" style="margin-top:auto;">Download!</button>
  32. </div>
  33. </div>
  34. <script type="text/javascript">
  35. // Add the following code if you want the name of the file appear on select
  36. $(".custom-file-input").on("change", function() {
  37. var fileName = $(this).val().split("\\").pop();
  38. $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
  39. });
  40. function copyToClipboard() {
  41. var copyText = document.getElementById("output1");
  42. copyText.select();
  43. document.execCommand("copy");
  44. }
  45. function download() {
  46. var url = document.getElementById('output3').src.replace(/^data:image\/[^;]+/, 'data:image/png');
  47. var link = document.createElement('a');
  48. link.href = url;
  49. link.download = 'image.png';
  50. document.body.appendChild(link);
  51. link.click();
  52. document.body.removeChild(link);
  53. }
  54. function toimage() {
  55. var string = document.getElementById('input3').value;
  56. var image = "data:image/png;base64," + string;
  57. document.getElementById('output3').src = image;
  58. document.getElementById('download').disabled = false;
  59. }
  60. function totext() {
  61. var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
  62. e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
  63. r = e.charCodeAt(f++);
  64. i = e.charCodeAt(f++);
  65. s = n >> 2;
  66. o = (n & 3) << 4 | r >> 4;
  67. u = (r & 15) << 2 | i >> 6;
  68. 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;
  69. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
  70. o = this._keyStr.indexOf(e.charAt(f++));
  71. u = this._keyStr.indexOf(e.charAt(f++));
  72. a = this._keyStr.indexOf(e.charAt(f++));
  73. n = s << 2 | o >> 4;
  74. r = (o & 15) << 4 | u >> 2;
  75. i = (u & 3) << 6 | a;
  76. 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);
  77. t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
  78. t += String.fromCharCode(r >> 6 & 63 | 128);
  79. 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);
  80. n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
  81. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  82. n += 2 } else { c2 = e.charCodeAt(n + 1);
  83. c3 = e.charCodeAt(n + 2);
  84. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  85. n += 3 } } return t } }
  86. // Define the string
  87. var string = document.getElementById('input4').value;
  88. // Encode the String
  89. var decodedString = Base64.decode(string);
  90. document.getElementById('output4').value = decodedString;
  91. }
  92. function tobase64() {
  93. var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
  94. e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
  95. r = e.charCodeAt(f++);
  96. i = e.charCodeAt(f++);
  97. s = n >> 2;
  98. o = (n & 3) << 4 | r >> 4;
  99. u = (r & 15) << 2 | i >> 6;
  100. 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;
  101. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
  102. o = this._keyStr.indexOf(e.charAt(f++));
  103. u = this._keyStr.indexOf(e.charAt(f++));
  104. a = this._keyStr.indexOf(e.charAt(f++));
  105. n = s << 2 | o >> 4;
  106. r = (o & 15) << 4 | u >> 2;
  107. i = (u & 3) << 6 | a;
  108. 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);
  109. t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
  110. t += String.fromCharCode(r >> 6 & 63 | 128);
  111. 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);
  112. n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
  113. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  114. n += 2 } else { c2 = e.charCodeAt(n + 1);
  115. c3 = e.charCodeAt(n + 2);
  116. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  117. n += 3 } } return t } }
  118. // Define the string
  119. var string = document.getElementById('input2').value;
  120. // Encode the String
  121. var encodedString = Base64.encode(string);
  122. document.getElementById('output2').value = encodedString;
  123. }
  124. </script>
  125. <script>
  126. function handleFileSelect(evt) {
  127. document.getElementById("loading").style.display = "";
  128. var files = evt.target.files; // FileList object
  129. // Loop through the FileList and render image files as thumbnails.
  130. for (var i = 0, f; f = files[i]; i++) {
  131. // Only process image files.
  132. if (!f.type.match('image.*')) {
  133. continue;
  134. }
  135. var reader = new FileReader();
  136. // Closure to capture the file information.
  137. reader.onload = (function(theFile) {
  138. return function(e) {
  139. // Render thumbnail.
  140. document.getElementById('imageb64').src = e.target.result;
  141. document.getElementById("output3").value = "url('" + e.target.result + "')";
  142. document.getElementById("output2").value = e.target.result;
  143. document.getElementById("output1").value = e.target.result.split(',')[1];
  144. imageB64 = e.target.result;
  145. document.getElementById("loading").style.display = "none";
  146. };
  147. })(f);
  148. // Read in the image file as a data URL.
  149. reader.readAsDataURL(f);
  150. }
  151. }
  152. //document.getElementById('files').addEventListener('change', handleFileSelect, false);
  153. </script>
  154. {% endblock %}