Image.js 3.1 KB

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