You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB

2 years ago
  1. export function convertImgToBase64(url) {
  2. var canvas = document.createElement('canvas');
  3. var ctx = canvas.getContext('2d');
  4. // img = document.createElement('img'),
  5. var img = new Image();
  6. img.src = url;
  7. canvas.height = img.height;
  8. canvas.width = img.width;
  9. var dataURL = canvas.toDataURL('image/jpeg');
  10. alert(dataURL);
  11. canvas = null;
  12. return dataURL;
  13. }
  14. export function getDataUri(url, cb) {
  15. var image = new Image();
  16. image.setAttribute('crossOrigin', 'anonymous'); //getting images from external domain
  17. image.onload = function () {
  18. var canvas = document.createElement('canvas');
  19. canvas.width = this.naturalWidth;
  20. canvas.height = this.naturalHeight;
  21. //next three lines for white background in case png has a transparent background
  22. var ctx = canvas.getContext('2d');
  23. ctx.fillStyle = '#fff'; /// set white fill style
  24. ctx.fillRect(0, 0, canvas.width, canvas.height);
  25. canvas.getContext('2d').drawImage(this, 0, 0);
  26. cb(canvas.toDataURL('image/jpeg'));
  27. };
  28. image.src = url;
  29. }
  30. // export function toDataURL(url, callback) {
  31. // var XMLHttpRequest = require('xhr2');
  32. // var xhr = new XMLHttpRequest();
  33. // xhr.onload = function () {
  34. // var reader = new FileReader();
  35. // reader.onloadend = function () {
  36. // callback(reader.result);
  37. // };
  38. // reader.readAsDataURL(xhr.response);
  39. // };
  40. // xhr.open("GET", url);
  41. // xhr.responseType = "blob";
  42. // xhr.send();
  43. // }