You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.9 KiB

  1. import EXIF from 'exif-js';
  2. const MAX_IMAGE_PIXELS = 1638400; // 1280x1280px
  3. const _browser_quirks = {};
  4. // Some browsers will automatically draw images respecting their EXIF orientation
  5. // while others won't, and the safest way to detect that is to examine how it
  6. // is done on a known image.
  7. // See https://github.com/w3c/csswg-drafts/issues/4666
  8. // and https://github.com/blueimp/JavaScript-Load-Image/commit/1e4df707821a0afcc11ea0720ee403b8759f3881
  9. const dropOrientationIfNeeded = (orientation) => new Promise(resolve => {
  10. switch (_browser_quirks['image-orientation-automatic']) {
  11. case true:
  12. resolve(1);
  13. break;
  14. case false:
  15. resolve(orientation);
  16. break;
  17. default:
  18. // black 2x1 JPEG, with the following meta information set:
  19. // - EXIF Orientation: 6 (Rotated 90° CCW)
  20. const testImageURL =
  21. 'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +
  22. 'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +
  23. 'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +
  24. 'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +
  25. 'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +
  26. 'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q==';
  27. const img = new Image();
  28. img.onload = () => {
  29. const automatic = (img.width === 1 && img.height === 2);
  30. _browser_quirks['image-orientation-automatic'] = automatic;
  31. resolve(automatic ? 1 : orientation);
  32. };
  33. img.onerror = () => {
  34. _browser_quirks['image-orientation-automatic'] = false;
  35. resolve(orientation);
  36. };
  37. img.src = testImageURL;
  38. }
  39. });
  40. const getImageUrl = inputFile => new Promise((resolve, reject) => {
  41. if (window.URL && URL.createObjectURL) {
  42. try {
  43. resolve(URL.createObjectURL(inputFile));
  44. } catch (error) {
  45. reject(error);
  46. }
  47. return;
  48. }
  49. const reader = new FileReader();
  50. reader.onerror = (...args) => reject(...args);
  51. reader.onload = ({ target }) => resolve(target.result);
  52. reader.readAsDataURL(inputFile);
  53. });
  54. const loadImage = inputFile => new Promise((resolve, reject) => {
  55. getImageUrl(inputFile).then(url => {
  56. const img = new Image();
  57. img.onerror = (...args) => reject(...args);
  58. img.onload = () => resolve(img);
  59. img.src = url;
  60. }).catch(reject);
  61. });
  62. const getOrientation = (img, type = 'image/png') => new Promise(resolve => {
  63. if (type !== 'image/jpeg') {
  64. resolve(1);
  65. return;
  66. }
  67. EXIF.getData(img, () => {
  68. const orientation = EXIF.getTag(img, 'Orientation');
  69. if (orientation !== 1) {
  70. dropOrientationIfNeeded(orientation).then(resolve).catch(() => resolve(orientation));
  71. } else {
  72. resolve(orientation);
  73. }
  74. });
  75. });
  76. const processImage = (img, { width, height, orientation, type = 'image/png' }) => new Promise(resolve => {
  77. const canvas = document.createElement('canvas');
  78. if (4 < orientation && orientation < 9) {
  79. canvas.width = height;
  80. canvas.height = width;
  81. } else {
  82. canvas.width = width;
  83. canvas.height = height;
  84. }
  85. const context = canvas.getContext('2d');
  86. switch (orientation) {
  87. case 2: context.transform(-1, 0, 0, 1, width, 0); break;
  88. case 3: context.transform(-1, 0, 0, -1, width, height); break;
  89. case 4: context.transform(1, 0, 0, -1, 0, height); break;
  90. case 5: context.transform(0, 1, 1, 0, 0, 0); break;
  91. case 6: context.transform(0, 1, -1, 0, height, 0); break;
  92. case 7: context.transform(0, -1, -1, 0, height, width); break;
  93. case 8: context.transform(0, -1, 1, 0, 0, width); break;
  94. }
  95. context.drawImage(img, 0, 0, width, height);
  96. // The Tor Browser and maybe other browsers may prevent reading from canvas
  97. // and return an all-white image instead. Assume reading failed if the resized
  98. // image is perfectly white.
  99. const imageData = context.getImageData(0, 0, width, height);
  100. if (imageData.data.every(value => value === 255)) {
  101. throw 'Failed to read from canvas';
  102. }
  103. canvas.toBlob(resolve, type);
  104. });
  105. const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => {
  106. const { width, height } = img;
  107. const newWidth = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (width / height)));
  108. const newHeight = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (height / width)));
  109. getOrientation(img, type)
  110. .then(orientation => processImage(img, {
  111. width: newWidth,
  112. height: newHeight,
  113. orientation,
  114. type,
  115. }))
  116. .then(resolve)
  117. .catch(reject);
  118. });
  119. export default inputFile => new Promise((resolve, reject) => {
  120. if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
  121. resolve(inputFile);
  122. return;
  123. }
  124. loadImage(inputFile).then(img => {
  125. if (img.width * img.height < MAX_IMAGE_PIXELS) {
  126. resolve(inputFile);
  127. return;
  128. }
  129. resizeImage(img, inputFile.type)
  130. .then(resolve)
  131. .catch(() => resolve(inputFile));
  132. }).catch(reject);
  133. });