img2html.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <html>
  2. <head>
  3. <title>Image-to-html converter</title>
  4. <style>
  5. #img, #canvas, #span {
  6. display: none;
  7. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAL0lEQVQ4jWP09vZ+xoAHuLi44JNmYMIrSwQYNWAwGMBCKJ737NlDWxeMGjAYDAAAak4FtfgpsBoAAAAASUVORK5CYII=);
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h2>Image-to-html converter</h2>
  13. <p>Enter the relative path to an image file, and this will convert it
  14. to a pure HTML representation (no images).</p>
  15. <form onsubmit="start_convert(); return false;">
  16. Path to image: <input type="text" id="filepath" size="60"><br>
  17. <input id="fill" type="checkbox">
  18. Fill canvas with <input id="fillRGB" value="rgb(10,100,250)"> (instead of transparency).<br>
  19. <button type='submit'>Convert!</button>
  20. <br><br>
  21. <img id="img" onload="run_convert();"><canvas id="canvas"></canvas><span id="span"></span><br>
  22. (img / canvas/ imghtml)
  23. <br><br>
  24. Result:<br>
  25. <textarea id="textarea" rows="10" cols="80"></textarea>
  26. </form>
  27. <script>
  28. var img = document.getElementById("img");
  29. var canvas = document.getElementById("canvas");
  30. var span = document.getElementById("span");
  31. var textarea = document.getElementById("textarea");
  32. var fill = document.getElementById("fill");
  33. var fillRGB = document.getElementById("fillRGB");
  34. function start_convert() {
  35. try {
  36. // Unhide stuff. They're initially hidden because the image shows a
  37. // broken-image icon on first page load, and the canvas defaults to a
  38. // large empty area.
  39. img.style.display = "inline";
  40. canvas.style.display = "inline";
  41. span.style.display = "inline-block";
  42. // Clear out any previous values.
  43. textarea.value = "(loading image)"
  44. span.innerHTML = "";
  45. // Get the image filename
  46. var input = document.getElementById("filepath");
  47. img.src = input.value;
  48. // We're done, let the onload handler do the real work.
  49. } catch (e) {
  50. alert("Crap, start_convert failed: " + e);
  51. }
  52. }
  53. function run_convert() {
  54. try {
  55. textarea.value = "(rendering canvas)";
  56. canvas.width = img.width;
  57. canvas.height = img.height;
  58. var ctx = canvas.getContext("2d");
  59. ctx.clearRect(0, 0, img.width, img.height);
  60. if (fill.checked) {
  61. ctx.fillStyle = fillRGB.value;
  62. ctx.fillRect (0, 0, img.width, img.height);
  63. }
  64. ctx.drawImage(img, 0, 0);
  65. // [r, g, b, a, r, g, b, a, ...]
  66. var pixels = ctx.getImageData(0, 0, img.width, img.height).data;
  67. var imghtml = "<table cellpadding='0' cellspacing='0' width='" +
  68. img.width + "' height='" + img.height + "'>\n";
  69. for (var y = 0; y < img.height; y++) {
  70. imghtml += "<tr height='1'>\n";
  71. textarea.value = "(converting row " + y + ")";
  72. for (var x = 0; x < img.width; x++) {
  73. var p = img.width * y * 4 + x * 4;
  74. var r = pixels[p + 0];
  75. var g = pixels[p + 1];
  76. var b = pixels[p + 2];
  77. var a = pixels[p + 3];
  78. var alpha = (a / 255).toString();
  79. alpha = alpha.substring(0, 6); // "0.12345678 --> 0.1234"
  80. imghtml += " <td width='1' style='background-color: " +
  81. "rgba(" +
  82. r + "," +
  83. g + "," +
  84. b + "," +
  85. alpha +
  86. ")'></td>\n";
  87. }
  88. imghtml += "</tr>\n";
  89. }
  90. imghtml += "</table>\n";
  91. span.innerHTML = imghtml;
  92. textarea.value = "<html><body>\n" + imghtml + "</body></html>";
  93. } catch (e) {
  94. alert("Crap, run_convert failed: " + e);
  95. }
  96. }
  97. </script>
  98. </body>
  99. </html>