fileUtil.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import EXIF from 'exif-js'
  2. // 旋转图片
  3. const rotateImg = (img, direction, canvas) => {
  4. //最小与最大旋转方向,图片旋转4次后回到原方向
  5. const min_step = 0;
  6. const max_step = 3;
  7. if (img == null) return;
  8. //img的高度和宽度不能在img元素隐藏后获取,否则会出错
  9. let height = img.height;
  10. let width = img.width;
  11. let step = 2;
  12. if (step == null) {
  13. step = min_step;
  14. }
  15. if (direction == "right") {
  16. step++;
  17. //旋转到原位置,即超过最大值
  18. step > max_step && (step = min_step);
  19. } else {
  20. step--;
  21. step < min_step && (step = max_step);
  22. }
  23. //旋转角度以弧度值为参数
  24. let degree = (step * 90 * Math.PI) / 180;
  25. let ctx = canvas.getContext("2d");
  26. switch (step) {
  27. case 0:
  28. canvas.width = width;
  29. canvas.height = height;
  30. ctx.drawImage(img, 0, 0);
  31. break;
  32. case 1:
  33. canvas.width = height;
  34. canvas.height = width;
  35. ctx.rotate(degree);
  36. ctx.drawImage(img, 0, -height);
  37. break;
  38. case 2:
  39. canvas.width = width;
  40. canvas.height = height;
  41. ctx.rotate(degree);
  42. ctx.drawImage(img, -width, -height);
  43. break;
  44. case 3:
  45. canvas.width = height;
  46. canvas.height = width;
  47. ctx.rotate(degree);
  48. ctx.drawImage(img, -width, 0);
  49. break;
  50. }
  51. }
  52. export default {
  53. getOrientation: (file) => {
  54. return new Promise((resolve) => {
  55. EXIF.getData(file, function () {
  56. // console.log(EXIF.getAllTags(this))
  57. const orient = EXIF.getTag(this, 'Orientation')
  58. // console.log(orient)
  59. resolve(orient)
  60. })
  61. })
  62. },
  63. dataURLtoFile: (dataUrl, filename) => {
  64. const arr = dataUrl.split(',')
  65. const mime = arr[0].match(/:(.*?);/)[1]
  66. const bstr = atob(arr[1])
  67. let n = bstr.length
  68. let u8arr = new Uint8Array(n);
  69. while (n--) {
  70. u8arr[n] = bstr.charCodeAt(n);
  71. }
  72. return new File([u8arr], filename, {
  73. type: mime
  74. });
  75. },
  76. // rotateImage: (image, width, height) => {
  77. // let canvas = document.createElement('canvas')
  78. // let ctx = canvas.getContext('2d')
  79. // ctx.save()
  80. // canvas.width = height
  81. // canvas.height = width
  82. // ctx.rotate(90 * Math.PI / 180)
  83. // ctx.drawImage(image, 0, -height)
  84. // ctx.restore()
  85. // return canvas.toDataURL("image/jpeg")
  86. // },
  87. rotateImage: (img, width, height, Orientation) => {
  88. let canvas = document.createElement('canvas')
  89. let ctx = canvas.getContext('2d')
  90. ctx.save()
  91. canvas.width = height
  92. canvas.height = width
  93. //修复ios上传图片的时候 被旋转的问题
  94. if (Orientation != "" && Orientation != 1) {
  95. switch (Orientation) {
  96. case 6: //需要顺时针(向左)90度旋转
  97. rotateImg(img, "left", canvas);
  98. break;
  99. case 8: //需要逆时针(向右)90度旋转
  100. rotateImg(img, "right", canvas);
  101. break;
  102. case 3: //需要180度旋转
  103. rotateImg(img, "right", canvas); //转两次
  104. rotateImg(img, "right", canvas);
  105. break;
  106. }
  107. }
  108. // ctx.rotate(90 * Math.PI / 180)
  109. // ctx.drawImage(img, 0, -height)
  110. ctx.restore()
  111. return canvas.toDataURL("image/jpeg")
  112. }
  113. }