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.

106 lines
2.8 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 (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. 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. canvas.toBlob(resolve, type);
  56. });
  57. const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => {
  58. const { width, height } = img;
  59. const newWidth = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (width / height)));
  60. const newHeight = Math.round(Math.sqrt(MAX_IMAGE_PIXELS * (height / width)));
  61. getOrientation(img, type)
  62. .then(orientation => processImage(img, {
  63. width: newWidth,
  64. height: newHeight,
  65. orientation,
  66. type,
  67. }))
  68. .then(resolve)
  69. .catch(reject);
  70. });
  71. export default inputFile => new Promise((resolve, reject) => {
  72. if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') {
  73. resolve(inputFile);
  74. return;
  75. }
  76. loadImage(inputFile).then(img => {
  77. if (img.width * img.height < MAX_IMAGE_PIXELS) {
  78. resolve(inputFile);
  79. return;
  80. }
  81. resizeImage(img, inputFile.type)
  82. .then(resolve)
  83. .catch(() => resolve(inputFile));
  84. }).catch(reject);
  85. });