您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

getLessLimitSizeImage.js 1013B

12345678910111213141516171819202122232425262728293031323334
  1. // 压缩上传图片
  2. export function translate(imgSrc, scale, fileName, callback) {
  3. let img = new Image();
  4. img.src = imgSrc;
  5. img.onload = function() {
  6. let that = this;
  7. let h = that.height; // 默认按比例压缩
  8. let w = that.width;
  9. let canvas = document.createElement('canvas');
  10. let ctx = canvas.getContext('2d');
  11. let width = document.createAttribute("width");
  12. width.nodeValue = w;
  13. let height = document.createAttribute("height");
  14. height.nodeValue = h;
  15. canvas.setAttributeNode(width);
  16. canvas.setAttributeNode(height);
  17. ctx.drawImage(that, 0, 0, w, h);
  18. let dataurl = canvas.toDataURL('image/jpeg', scale); //压缩比例
  19. let arr = dataurl.split(','),
  20. mime = arr[0].match(/:(.*?);/)[1],
  21. bstr = atob(arr[1]),
  22. n = bstr.length,
  23. u8arr = new Uint8Array(n);
  24. while (n--) {
  25. u8arr[n] = bstr.charCodeAt(n);
  26. }
  27. let file = new File([u8arr], fileName, {
  28. type: mime
  29. });
  30. // file.lastModifiedDate = new Date();
  31. canvas = null;
  32. callback && callback(dataurl, file);
  33. }
  34. }