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

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