upload.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Message } from 'element-ui'
  2. /**
  3. *
  4. * @param {file} file 源文件
  5. * @desc 限制为图片文件
  6. * @retutn 是图片文件返回true否则返回false
  7. */
  8. export const isImageFile = (file, fileTypes) => {
  9. const types = fileTypes || [
  10. 'image/png',
  11. 'image/gif',
  12. 'image/jpeg',
  13. 'image/jpg',
  14. 'image/bmp',
  15. 'image/x-icon'
  16. ]
  17. const isImage = types.includes(file.type)
  18. if (!isImage) {
  19. Message.error('上传文件非图片格式!')
  20. return false
  21. }
  22. return true
  23. }
  24. /**
  25. *
  26. * @param {file} file 源文件
  27. * @param {number} fileMaxSize 图片限制大小单位(MB)
  28. * @desc 限制为文件上传大小
  29. * @retutn 在限制内返回true否则返回false
  30. */
  31. export const isMaxFileSize = (file, fileMaxSize = 2) => {
  32. const isMaxSize = file.size / 1024 / 1024 < fileMaxSize
  33. if (!isMaxSize) {
  34. Message.error('上传头像图片大小不能超过 ' + fileMaxSize + 'MB!')
  35. return false
  36. }
  37. return true
  38. }
  39. /**
  40. *
  41. * @param {file} file 源文件
  42. * @desc 读取图片文件为base64文件格式
  43. * @retutn 返回base64文件
  44. */
  45. export const readFile = file => {
  46. return new Promise((resolve, reject) => {
  47. const reader = new FileReader()
  48. reader.onload = e => {
  49. const data = e.target.result
  50. resolve(data)
  51. }
  52. reader.onerror = () => {
  53. const err = new Error('读取图片失败')
  54. reject(err.message)
  55. }
  56. reader.readAsDataURL(file)
  57. })
  58. }
  59. /**
  60. *
  61. * @param {string} src 图片地址
  62. * @desc 加载真实图片
  63. * @return 读取成功返回图片真实宽高对象 ag: {width:100,height:100}
  64. */
  65. export const loadImage = src => {
  66. return new Promise((resolve, reject) => {
  67. const image = new Image()
  68. image.src = src
  69. image.onload = () => {
  70. const data = {
  71. width: image.width,
  72. height: image.height
  73. }
  74. resolve(data)
  75. }
  76. image.onerror = () => {
  77. const err = new Error('加载图片失败')
  78. reject(err)
  79. }
  80. })
  81. }
  82. /**
  83. *
  84. * @param {file} file 源文件
  85. * @param {object} props 文件分辨率的宽和高 ag: props={width:100, height :100}
  86. * @desc 判断图片文件的分辨率是否在限定范围之内
  87. * @throw 分辨率不在限定范围之内则抛出异常
  88. *
  89. */
  90. export const isAppropriateResolution = async (file, props) => {
  91. try {
  92. const { width, height } = props
  93. const base64 = await readFile(file)
  94. const image = await loadImage(base64)
  95. if (image.width !== width || image.height !== height) {
  96. throw new Error('上传图片的分辨率必须为' + width + '*' + height)
  97. }
  98. } catch (error) {
  99. throw error
  100. }
  101. }
  102. /**
  103. *
  104. * @param {file} file 源文件
  105. * @param {array} ratio 限制的文件比例 ag: ratio= [1,1]
  106. * @desc 判断图片文件的比列是否在限定范围
  107. * @throw 比例不在限定范围之内则抛出异常
  108. */
  109. export const isAppRatio = async (file, ratio) => {
  110. try {
  111. const [w, h] = ratio
  112. if (h === 0 || w === 0) {
  113. const err = '上传图片的比例不能出现0'
  114. Message.error(err)
  115. throw new Error(err)
  116. }
  117. const base64 = await readFile(file)
  118. const image = await loadImage(base64)
  119. if (image.width / image.height !== w / h) {
  120. throw new Error('上传图片的宽高比例必须为 ' + w + ' : ' + h)
  121. }
  122. } catch (error) {
  123. throw error
  124. }
  125. }