Image.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import * as ExifParser from "exif-parser";
  2. import removeEXIF from "../vendor/remove-exif.js";
  3. import Utils from "../Utils.js";
  4. import FileType from "./FileType.js";
  5. import {fromBase64, toBase64} from "../lib/Base64";
  6. import {fromHex} from "../lib/Hex";
  7. /**
  8. * Image operations.
  9. *
  10. * @author tlwr [toby@toby.codes]
  11. * @copyright Crown Copyright 2017
  12. * @license Apache-2.0
  13. *
  14. * @namespace
  15. */
  16. const Image = {
  17. /**
  18. * Extract EXIF operation.
  19. *
  20. * Extracts EXIF data from a byteArray, representing a JPG or a TIFF image.
  21. *
  22. * @param {ArrayBuffer} input
  23. * @param {Object[]} args
  24. * @returns {string}
  25. */
  26. runExtractEXIF(input, args) {
  27. try {
  28. const parser = ExifParser.create(input);
  29. const result = parser.parse();
  30. let lines = [];
  31. for (let tagName in result.tags) {
  32. let value = result.tags[tagName];
  33. lines.push(`${tagName}: ${value}`);
  34. }
  35. const numTags = lines.length;
  36. lines.unshift(`Found ${numTags} tags.\n`);
  37. return lines.join("\n");
  38. } catch (err) {
  39. throw "Could not extract EXIF data from image: " + err;
  40. }
  41. },
  42. /**
  43. * Remove EXIF operation.
  44. *
  45. * Removes EXIF data from a byteArray, representing a JPG.
  46. *
  47. * @author David Moodie [davidmoodie12@gmail.com]
  48. * @param {byteArray} input
  49. * @param {Object[]} args
  50. * @returns {byteArray}
  51. */
  52. runRemoveEXIF(input, args) {
  53. // Do nothing if input is empty
  54. if (input.length === 0) return input;
  55. try {
  56. return removeEXIF(input);
  57. } catch (err) {
  58. // Simply return input if no EXIF data is found
  59. if (err === "Exif not found.") return input;
  60. throw "Could not remove EXIF data from image: " + err;
  61. }
  62. },
  63. /**
  64. * @constant
  65. * @default
  66. */
  67. INPUT_FORMAT: ["Raw", "Base64", "Hex"],
  68. /**
  69. * Render Image operation.
  70. *
  71. * @author n1474335 [n1474335@gmail.com]
  72. * @param {string} input
  73. * @param {Object[]} args
  74. * @returns {html}
  75. */
  76. runRenderImage(input, args) {
  77. const inputFormat = args[0];
  78. let dataURI = "data:";
  79. if (!input.length) return "";
  80. // Convert input to raw bytes
  81. switch (inputFormat) {
  82. case "Hex":
  83. input = fromHex(input);
  84. break;
  85. case "Base64":
  86. // Don't trust the Base64 entered by the user.
  87. // Unwrap it first, then re-encode later.
  88. input = fromBase64(input, null, "byteArray");
  89. break;
  90. case "Raw":
  91. default:
  92. input = Utils.strToByteArray(input);
  93. break;
  94. }
  95. // Determine file type
  96. const type = FileType.magicType(input);
  97. if (type && type.mime.indexOf("image") === 0) {
  98. dataURI += type.mime + ";";
  99. } else {
  100. throw "Invalid file type";
  101. }
  102. // Add image data to URI
  103. dataURI += "base64," + toBase64(input);
  104. return "<img src='" + dataURI + "'>";
  105. },
  106. };
  107. export default Image;