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.

functions.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // @flow
  2. let filterSupport;
  3. /**
  4. * Checks context filter support.
  5. *
  6. * @returns {boolean} True if the filter is supported and false if the filter is not supported by the browser.
  7. */
  8. export function checkBlurSupport() {
  9. if (typeof filterSupport === 'undefined') {
  10. const canvas = document.createElement('canvas');
  11. const ctx = canvas.getContext('2d');
  12. filterSupport = typeof ctx.filter !== 'undefined';
  13. canvas.remove();
  14. }
  15. return filterSupport;
  16. }
  17. /**
  18. * Convert blob to base64.
  19. *
  20. * @param {Blob} blob - The link to add info with.
  21. * @returns {Promise<string>}
  22. */
  23. export const blobToData = (blob: Blob): Promise<string> =>
  24. new Promise(resolve => {
  25. const reader = new FileReader();
  26. reader.onloadend = () => resolve(reader.result.toString());
  27. reader.readAsDataURL(blob);
  28. });
  29. /**
  30. * Convert blob to base64.
  31. *
  32. * @param {string} url - The image url.
  33. * @returns {Object} - Returns the converted blob to base64.
  34. */
  35. export const toDataURL = async (url: string) => {
  36. const response = await fetch(url);
  37. const blob = await response.blob();
  38. const resData = await blobToData(blob);
  39. return resData;
  40. };
  41. /**
  42. * Resize image and adjust original aspect ratio.
  43. *
  44. * @param {Object} base64image - Base64 image extraction.
  45. * @param {number} width - Value for resizing the image width.
  46. * @param {number} height - Value for resizing the image height.
  47. * @returns {Promise<string>}
  48. *
  49. */
  50. export function resizeImage(base64image: any, width: number = 1920, height: number = 1080): Promise<string> {
  51. // In order to work on Firefox browser we need to handle the asynchronous nature of image loading; We need to use
  52. // a promise mechanism. The reason why it 'works' without this mechanism in Chrome is actually 'by accident' because
  53. // the image happens to be in the cache and the browser is able to deliver the uncompressed/decoded image
  54. // before using the image in the drawImage call.
  55. return new Promise(resolve => {
  56. const img = document.createElement('img');
  57. img.onload = function() {
  58. // Create an off-screen canvas.
  59. const canvas = document.createElement('canvas');
  60. // Set its dimension to target size.
  61. const context = canvas.getContext('2d');
  62. canvas.width = width;
  63. canvas.height = height;
  64. // Draw source image into the off-screen canvas.
  65. // TODO: keep aspect ratio and implement object-fit: cover.
  66. context.drawImage(img, 0, 0, width, height);
  67. // Encode image to data-uri with base64 version of compressed image.
  68. resolve(canvas.toDataURL('image/jpeg', 0.5));
  69. };
  70. img.src = base64image;
  71. });
  72. }