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

getLessLimitSizeImage.js 681B

12345678910111213141516171819202122
  1. // 压缩上传图片
  2. export function translate(imgSrc, scale, 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. var base64 = canvas.toDataURL('image/jpeg', scale); //压缩比例
  19. canvas = null;
  20. callback(base64);
  21. }
  22. }