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.

127 lines
3.0 KiB

  1. import EXIF from 'exif-js';
  2. const MAX_IMAGE_DIMENSION = 1280;
  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 (type !== 'image/jpeg') {
  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. [canvas.width, canvas.height] = orientation < 5 ? [width, height] : [height, width];
  38. const context = canvas.getContext('2d');
  39. switch (orientation) {
  40. case 2:
  41. context.translate(width, 0);
  42. break;
  43. case 3:
  44. context.translate(width, height);
  45. break;
  46. case 4:
  47. context.translate(0, height);
  48. break;
  49. case 5:
  50. context.rotate(0.5 * Math.PI);
  51. context.translate(1, -1);
  52. break;
  53. case 6:
  54. context.rotate(0.5 * Math.PI);
  55. context.translate(0, -height);
  56. break;
  57. case 7:
  58. context.rotate(0.5, Math.PI);
  59. context.translate(width, -height);
  60. break;
  61. case 8:
  62. context.rotate(-0.5, Math.PI);
  63. context.translate(-width, 0);
  64. break;
  65. }
  66. context.drawImage(img, 0, 0, width, height);
  67. canvas.toBlob(resolve, type);
  68. });
  69. const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => {
  70. const { width, height } = img;
  71. let newWidth, newHeight;
  72. if (width > height) {
  73. newHeight = height * MAX_IMAGE_DIMENSION / width;
  74. newWidth = MAX_IMAGE_DIMENSION;
  75. } else if (height > width) {
  76. newWidth = width * MAX_IMAGE_DIMENSION / height;
  77. newHeight = MAX_IMAGE_DIMENSION;
  78. } else {
  79. newWidth = MAX_IMAGE_DIMENSION;
  80. newHeight = MAX_IMAGE_DIMENSION;
  81. }
  82. getOrientation(img, type)
  83. .then(orientation => processImage(img, {
  84. width: newWidth,
  85. height: newHeight,
  86. orientation,
  87. type,
  88. }))
  89. .then(resolve)
  90. .catch(reject);
  91. });
  92. export default inputFile => new Promise((resolve, reject) => {
  93. if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
  94. resolve(inputFile);
  95. return;
  96. }
  97. loadImage(inputFile).then(img => {
  98. if (img.width < MAX_IMAGE_DIMENSION && img.height < MAX_IMAGE_DIMENSION) {
  99. resolve(inputFile);
  100. return;
  101. }
  102. resizeImage(img, inputFile.type)
  103. .then(resolve)
  104. .catch(() => resolve(inputFile));
  105. }).catch(reject);
  106. });