|
@@ -25,12 +25,13 @@ export function checkBlurSupport() {
|
25
|
25
|
* @param {Blob} blob - The link to add info with.
|
26
|
26
|
* @returns {Promise<string>}
|
27
|
27
|
*/
|
28
|
|
-export const blobToData = (blob: Blob): Promise<string> => new Promise(resolve => {
|
29
|
|
- const reader = new FileReader();
|
|
28
|
+export const blobToData = (blob: Blob): Promise<string> =>
|
|
29
|
+ new Promise(resolve => {
|
|
30
|
+ const reader = new FileReader();
|
30
|
31
|
|
31
|
|
- reader.onloadend = () => resolve(reader.result.toString());
|
32
|
|
- reader.readAsDataURL(blob);
|
33
|
|
-});
|
|
32
|
+ reader.onloadend = () => resolve(reader.result.toString());
|
|
33
|
+ reader.readAsDataURL(blob);
|
|
34
|
+ });
|
34
|
35
|
|
35
|
36
|
/**
|
36
|
37
|
* Convert blob to base64.
|
|
@@ -52,28 +53,35 @@ export const toDataURL = async (url: string) => {
|
52
|
53
|
* @param {Object} base64image - Base64 image extraction.
|
53
|
54
|
* @param {number} width - Value for resizing the image width.
|
54
|
55
|
* @param {number} height - Value for resizing the image height.
|
55
|
|
- * @returns {Object} Returns the canvas output.
|
|
56
|
+ * @returns {Promise<string>}
|
56
|
57
|
*
|
57
|
58
|
*/
|
58
|
|
-export async function resizeImage(base64image: any, width: number = 1920, height: number = 1080) {
|
59
|
|
- const img = document.createElement('img');
|
|
59
|
+export function resizeImage(base64image: any, width: number = 1920, height: number = 1080): Promise<string> {
|
|
60
|
+
|
|
61
|
+ // In order to work on Firefox browser we need to handle the asynchronous nature of image loading; We need to use
|
|
62
|
+ // a promise mechanism. The reason why it 'works' without this mechanism in Chrome is actually 'by accident' because
|
|
63
|
+ // the image happens to be in the cache and the browser is able to deliver the uncompressed/decoded image
|
|
64
|
+ // before using the image in the drawImage call.
|
|
65
|
+ return new Promise(resolve => {
|
|
66
|
+ const img = document.createElement('img');
|
60
|
67
|
|
61
|
|
- img.src = base64image;
|
62
|
|
- /* eslint-disable no-empty-function */
|
63
|
|
- img.onload = await function() {};
|
|
68
|
+ img.onload = function() {
|
|
69
|
+ // Create an off-screen canvas.
|
|
70
|
+ const canvas = document.createElement('canvas');
|
64
|
71
|
|
65
|
|
- // Create an off-screen canvas.
|
66
|
|
- const canvas = document.createElement('canvas');
|
67
|
|
- const ctx = canvas.getContext('2d');
|
|
72
|
+ // Set its dimension to target size.
|
|
73
|
+ const context = canvas.getContext('2d');
|
68
|
74
|
|
69
|
|
- // Set its dimension to target size.
|
70
|
|
- canvas.width = width;
|
71
|
|
- canvas.height = height;
|
|
75
|
+ canvas.width = width;
|
|
76
|
+ canvas.height = height;
|
72
|
77
|
|
73
|
|
- // Draw source image into the off-screen canvas.
|
74
|
|
- // TODO: keep aspect ratio and implement object-fit: cover.
|
75
|
|
- ctx.drawImage(img, 0, 0, width, height);
|
|
78
|
+ // Draw source image into the off-screen canvas.
|
|
79
|
+ // TODO: keep aspect ratio and implement object-fit: cover.
|
|
80
|
+ context.drawImage(img, 0, 0, width, height);
|
76
|
81
|
|
77
|
|
- // Encode image to data-uri with base64 version of compressed image.
|
78
|
|
- return canvas.toDataURL('image/jpeg', 0.5);
|
|
82
|
+ // Encode image to data-uri with base64 version of compressed image.
|
|
83
|
+ resolve(canvas.toDataURL('image/jpeg', 0.5));
|
|
84
|
+ };
|
|
85
|
+ img.src = base64image;
|
|
86
|
+ });
|
79
|
87
|
}
|