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.

114 lines
3.2 KiB

  1. import EXIF from 'exif-js';
  2. const MAX_IMAGE_PIXELS = 1638400; // 1280x1280px
  3. const getImageUrl = inputFile => new Promise((resolve, reject) => {
  4. if (window.URL && URL.createObjectURL) {
  5. try {
  6. resolve(URL.createObjectURL(inputFile));
  7. } catch (error) {
  8. reject(error);
  9. }
  10. return;
  11. }
  12. const reader = new FileReader();
  13. reader.onerror = (...args) => reject(...args);
  14. reader.onload = ({ target }) => resolve(target.result);
  15. reader.readAsDataURL(inputFile);
  16. });
  17. const loadImage = inputFile => new Promise((resolve, reject) => {
  18. getImageUrl(inputFile).then(url => {
  19. const img = new Image();
  20. img.onerror = (...args) => reject(...args);
  21. img.onload = () => resolve(img);
  22. img.src = url;
  23. }).catch(reject);
  24. });
  25. const getOrientation = (img, type = 'image/png') => new Promise(resolve => {
  26. if (!['image/jpeg', 'image/webp'].includes(type)) {
  27. resolve(1);
  28. return;
  29. }
  30. EXIF.getData(img, () => {
  31. const orientation = EXIF.getTag(img, 'Orientation');
  32. resolve(orientation);
  33. });
  34. });
  35. const processImage = (img, { width, height, orientation, type = 'image/png' }) => new Promise(resolve => {
  36. const canvas = document.createElement('canvas');
  37. if (4 < orientation && orientation < 9) {
  38. canvas.width = height;
  39. canvas.height = width;
  40. } else {
  41. canvas.width = width;
  42. canvas.height = height;
  43. }
  44. const context = canvas.getContext('2d');
  45. switch (orientation) {
  46. case 2: context.transform(-1, 0, 0, 1, width, 0); break;
  47. case 3: context.transform(-1, 0, 0, -1, width, height); break;
  48. case 4: context.transform(1, 0, 0, -1, 0, height); break;
  49. case 5: context.transform(0, 1, 1, 0, 0, 0); break;
  50. case 6: context.transform(0, 1, -1, 0, height, 0); break;
  51. case 7: context.transform(0, -1, -1, 0, height, width); break;
  52. case 8: context.transform(0, -1, 1, 0, 0, width); break;
  53. }
  54. context.drawImage(img, 0, 0, width, height);
  55. // The Tor Browser and maybe other browsers may prevent reading from canvas
  56. // and return an all-white image instead. Assume reading failed if the resized
  57. // image is perfectly white.
  58. const imageData = context.getImageData(0, 0, width, height);
  59. if (imageData.data.every(value => value === 255)) {
  60. throw 'Failed to read from canvas';
  61. }
  62. canvas.toBlob(resolve, type);
  63. });
  64. const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => {
  65. const { width, height } = img;
  66. const newWidth = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (width / height)));
  67. const newHeight = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (height / width)));
  68. getOrientation(img, type)
  69. .then(orientation => processImage(img, {
  70. width: newWidth,
  71. height: newHeight,
  72. orientation,
  73. type,
  74. }))
  75. .then(resolve)
  76. .catch(reject);
  77. });
  78. export default inputFile => new Promise((resolve, reject) => {
  79. if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
  80. resolve(inputFile);
  81. return;
  82. }
  83. loadImage(inputFile).then(img => {
  84. if (img.width * img.height < MAX_IMAGE_PIXELS) {
  85. resolve(inputFile);
  86. return;
  87. }
  88. resizeImage(img, inputFile.type)
  89. .then(resolve)
  90. .catch(() => resolve(inputFile));
  91. }).catch(reject);
  92. });