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

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}
  3. ToBase64.dev - Encode Text to base64
  4. {% endblock %}
  5. {% block h1 %}Encode text to base64{% 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="input2" name="input2" 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="tobase64()" 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. <textarea class="form-control" id="output2" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
  21. </div>
  22. <button id="download" type="button" class="btn btn-info" onclick="copyToClipboard('output2')" style="margin-top:auto;">Copy to clipboard</button>
  23. </div>
  24. </div>
  25. <script type="text/javascript">
  26. // Add the following code if you want the name of the file appear on select
  27. $(".custom-file-input").on("change", function() {
  28. var fileName = $(this).val().split("\\").pop();
  29. $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
  30. });
  31. function copyToClipboard(element) {
  32. var copyText = document.getElementById(element);
  33. copyText.select();
  34. document.execCommand("copy");
  35. }
  36. function download() {
  37. var url = document.getElementById('output3').src.replace(/^data:image\/[^;]+/, 'data:image/png');
  38. window.open(url);
  39. }
  40. function toimage() {
  41. var string = document.getElementById('input3').value;
  42. var image = "data:image/png;base64," + string;
  43. document.getElementById('output3').src = image;
  44. document.getElementById('download').disabled = false;
  45. }
  46. function totext() {
  47. var Base64 = {
  48. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  49. encode: function(e) {
  50. var t = "";
  51. var n, r, i, s, o, u, a;
  52. var f = 0;
  53. e = Base64._utf8_encode(e);
  54. while (f < e.length) {
  55. n = e.charCodeAt(f++);
  56. r = e.charCodeAt(f++);
  57. i = e.charCodeAt(f++);
  58. s = n >> 2;
  59. o = (n & 3) << 4 | r >> 4;
  60. u = (r & 15) << 2 | i >> 6;
  61. a = i & 63;
  62. 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)
  63. }
  64. return t
  65. },
  66. decode: function(e) {
  67. var t = "";
  68. var n, r, i;
  69. var s, o, u, a;
  70. var f = 0;
  71. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, "");
  72. while (f < e.length) {
  73. s = this._keyStr.indexOf(e.charAt(f++));
  74. o = this._keyStr.indexOf(e.charAt(f++));
  75. u = this._keyStr.indexOf(e.charAt(f++));
  76. a = this._keyStr.indexOf(e.charAt(f++));
  77. n = s << 2 | o >> 4;
  78. r = (o & 15) << 4 | u >> 2;
  79. i = (u & 3) << 6 | a;
  80. t = t + String.fromCharCode(n);
  81. if (u != 64) { t = t + String.fromCharCode(r) }
  82. if (a != 64) { t = t + String.fromCharCode(i) }
  83. }
  84. t = Base64._utf8_decode(t);
  85. return t
  86. },
  87. _utf8_encode: function(e) {
  88. e = e.replace(/\r\n/g, "\n");
  89. var t = "";
  90. for (var n = 0; n < e.length; n++) {
  91. var r = e.charCodeAt(n);
  92. if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) {
  93. t += String.fromCharCode(r >> 6 | 192);
  94. t += String.fromCharCode(r & 63 | 128)
  95. } else {
  96. t += String.fromCharCode(r >> 12 | 224);
  97. t += String.fromCharCode(r >> 6 & 63 | 128);
  98. t += String.fromCharCode(r & 63 | 128)
  99. }
  100. }
  101. return t
  102. },
  103. _utf8_decode: function(e) {
  104. var t = "";
  105. var n = 0;
  106. var r = c1 = c2 = 0;
  107. while (n < e.length) {
  108. r = e.charCodeAt(n);
  109. if (r < 128) {
  110. t += String.fromCharCode(r);
  111. n++
  112. } else if (r > 191 && r < 224) {
  113. c2 = e.charCodeAt(n + 1);
  114. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  115. n += 2
  116. } else {
  117. c2 = e.charCodeAt(n + 1);
  118. c3 = e.charCodeAt(n + 2);
  119. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  120. n += 3
  121. }
  122. }
  123. return t
  124. }
  125. }
  126. // Define the string
  127. var string = document.getElementById('input4').value;
  128. // Encode the String
  129. var decodedString = Base64.decode(string);
  130. document.getElementById('output4').value = decodedString;
  131. }
  132. function tobase64() {
  133. var Base64 = {
  134. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  135. encode: function(e) {
  136. var t = "";
  137. var n, r, i, s, o, u, a;
  138. var f = 0;
  139. e = Base64._utf8_encode(e);
  140. while (f < e.length) {
  141. n = e.charCodeAt(f++);
  142. r = e.charCodeAt(f++);
  143. i = e.charCodeAt(f++);
  144. s = n >> 2;
  145. o = (n & 3) << 4 | r >> 4;
  146. u = (r & 15) << 2 | i >> 6;
  147. a = i & 63;
  148. 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)
  149. }
  150. return t
  151. },
  152. decode: function(e) {
  153. var t = "";
  154. var n, r, i;
  155. var s, o, u, a;
  156. var f = 0;
  157. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, "");
  158. while (f < e.length) {
  159. s = this._keyStr.indexOf(e.charAt(f++));
  160. o = this._keyStr.indexOf(e.charAt(f++));
  161. u = this._keyStr.indexOf(e.charAt(f++));
  162. a = this._keyStr.indexOf(e.charAt(f++));
  163. n = s << 2 | o >> 4;
  164. r = (o & 15) << 4 | u >> 2;
  165. i = (u & 3) << 6 | a;
  166. t = t + String.fromCharCode(n);
  167. if (u != 64) { t = t + String.fromCharCode(r) }
  168. if (a != 64) { t = t + String.fromCharCode(i) }
  169. }
  170. t = Base64._utf8_decode(t);
  171. return t
  172. },
  173. _utf8_encode: function(e) {
  174. e = e.replace(/\r\n/g, "\n");
  175. var t = "";
  176. for (var n = 0; n < e.length; n++) {
  177. var r = e.charCodeAt(n);
  178. if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) {
  179. t += String.fromCharCode(r >> 6 | 192);
  180. t += String.fromCharCode(r & 63 | 128)
  181. } else {
  182. t += String.fromCharCode(r >> 12 | 224);
  183. t += String.fromCharCode(r >> 6 & 63 | 128);
  184. t += String.fromCharCode(r & 63 | 128)
  185. }
  186. }
  187. return t
  188. },
  189. _utf8_decode: function(e) {
  190. var t = "";
  191. var n = 0;
  192. var r = c1 = c2 = 0;
  193. while (n < e.length) {
  194. r = e.charCodeAt(n);
  195. if (r < 128) {
  196. t += String.fromCharCode(r);
  197. n++
  198. } else if (r > 191 && r < 224) {
  199. c2 = e.charCodeAt(n + 1);
  200. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  201. n += 2
  202. } else {
  203. c2 = e.charCodeAt(n + 1);
  204. c3 = e.charCodeAt(n + 2);
  205. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  206. n += 3
  207. }
  208. }
  209. return t
  210. }
  211. }
  212. // Define the string
  213. var string = document.getElementById('input2').value;
  214. // Encode the String
  215. var encodedString = Base64.encode(string);
  216. document.getElementById('output2').value = encodedString;
  217. }
  218. </script>
  219. <script>
  220. function handleFileSelect(evt) {
  221. document.getElementById("loading").style.display = "";
  222. var files = evt.target.files; // FileList object
  223. // Loop through the FileList and render image files as thumbnails.
  224. for (var i = 0, f; f = files[i]; i++) {
  225. // Only process image files.
  226. if (!f.type.match('image.*')) {
  227. continue;
  228. }
  229. var reader = new FileReader();
  230. // Closure to capture the file information.
  231. reader.onload = (function(theFile) {
  232. return function(e) {
  233. // Render thumbnail.
  234. document.getElementById('imageb64').src = e.target.result;
  235. document.getElementById("output3").value = "url('" + e.target.result + "')";
  236. document.getElementById("output2").value = e.target.result;
  237. document.getElementById("output1").value = e.target.result.split(',')[1];
  238. imageB64 = e.target.result;
  239. document.getElementById("loading").style.display = "none";
  240. };
  241. })(f);
  242. // Read in the image file as a data URL.
  243. reader.readAsDataURL(f);
  244. }
  245. }
  246. //document.getElementById('files').addEventListener('change', handleFileSelect, false);
  247. </script>
  248. {% endblock %}