compressImg.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. export function showSize (base64url) {
  2. let str = base64url.replace('data:image/png;base64,', '')
  3. // 找到等号,把等号也去掉
  4. const equalIndex = str.indexOf('=')
  5. if (str.indexOf('=') > 0) {
  6. str = str.substring(0, equalIndex)
  7. }
  8. // 原来的字符流大小,单位为字节
  9. const strLength = base64url.length
  10. // 计算后得到的文件流大小,单位为字节
  11. const fileLength = parseInt(strLength - (strLength / 8) * 2)
  12. // 由字节转换为kb
  13. let size = ''
  14. size = (fileLength / 1024).toFixed(2)
  15. const sizeStr = size + '' // 转成字符串
  16. const index = sizeStr.indexOf('.') // 获取小数点处的索引
  17. const dou = sizeStr.substr(index + 1, 2) // 获取小数点后两位的值
  18. if (dou === '00') {
  19. // 判断后两位是否为00,如果是则删除00
  20. return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
  21. }
  22. return Number(size)
  23. }
  24. export function dataURLtoFile (dataurl, filename) {
  25. // 将base64转换为文件,dataurl为base64字符串,filename为文件名(必须带后缀名,如.jpg,.png)
  26. const arr = dataurl.split(',')
  27. const mime = arr[0].match(/:(.*?);/)[1]
  28. const bstr = atob(arr[1])
  29. let n = bstr.length
  30. const u8arr = new Uint8Array(n)
  31. while (n--) {
  32. u8arr[n] = bstr.charCodeAt(n)
  33. }
  34. return new File([u8arr], filename, { type: mime })
  35. }
  36. export function dataURLtoBlob (dataurl) {
  37. const arr = dataurl.split(',')
  38. const _arr = arr[1].substring(0, arr[1].length - 2)
  39. const mime = arr[0].match(/:(.*?);/)[1]
  40. const bstr = atob(_arr)
  41. let n = bstr.length
  42. const u8arr = new Uint8Array(n)
  43. while (n--) {
  44. u8arr[n] = bstr.charCodeAt(n)
  45. }
  46. return new Blob([u8arr], {
  47. type: mime
  48. })
  49. }
  50. export function translateBlobToBase64 (blob, callback) {
  51. const reader = new FileReader()
  52. reader.onload = function (e) {
  53. callback(e.target)
  54. }
  55. reader.readAsDataURL(blob)
  56. // 读取后,result属性中将包含一个data:URL格式的Base64字符串用来表示所读取的文件
  57. }
  58. // 压缩方法
  59. export function compressImage (base64, { width: w = 1280, height: h = 720, size = 200, quality = 1 }) {
  60. return new Promise((resolve, reject) => {
  61. const newImage = new Image()
  62. newImage.src = base64
  63. newImage.setAttribute('crossOrigin', 'Anonymous')
  64. let imgWidth, imgHeight
  65. newImage.onload = function () {
  66. imgWidth = 1280
  67. imgHeight = 720
  68. const canvas = document.createElement('canvas')
  69. const ctx = canvas.getContext('2d')
  70. if (Math.max(imgWidth, imgHeight) > w) {
  71. if (imgWidth > imgHeight) {
  72. canvas.width = w
  73. canvas.height = w * imgHeight / imgWidth
  74. } else {
  75. canvas.height = w
  76. canvas.width = w * imgWidth / imgHeight
  77. }
  78. } else {
  79. canvas.width = imgWidth
  80. canvas.height = imgHeight
  81. quality = 1
  82. }
  83. ctx.clearRect(0, 0, canvas.width, canvas.height)
  84. ctx.drawImage(this, 0, 0, canvas.width, canvas.height)
  85. let compressedBase64 = canvas.toDataURL('image/jpeg', quality) // 压缩语句
  86. if (showSize(compressedBase64) > Number(size)) {
  87. compressedBase64 = canvas.toDataURL('image/jpeg', quality - 0.2 > 0.4 ? quality - 0.2 : 0.4)
  88. }
  89. resolve(compressedBase64)
  90. }
  91. newImage.onerror = function () {
  92. reject(new Error('Failed to load image'))
  93. }
  94. })
  95. }