Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 2bb5968

Browse files
For Safari support, avoid use of createImageBitmap
1 parent db43dc1 commit 2bb5968

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

BlazorInputFile/wwwroot/inputfile.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,40 @@
3636

3737
toImageFile(elem, fileId, format, maxWidth, maxHeight) {
3838
var originalFile = getFileById(elem, fileId);
39-
return createImageBitmap(originalFile.blob)
40-
.then(function (imageBitmap) {
41-
return new Promise(function (resolve) {
42-
var desiredWidthRatio = Math.min(1, maxWidth / imageBitmap.width);
43-
var desiredHeightRatio = Math.min(1, maxHeight / imageBitmap.height);
44-
var chosenSizeRatio = Math.min(desiredWidthRatio, desiredHeightRatio);
45-
46-
var canvas = document.createElement('canvas');
47-
canvas.width = Math.round(imageBitmap.width * chosenSizeRatio);
48-
canvas.height = Math.round(imageBitmap.height * chosenSizeRatio);
49-
canvas.getContext('2d').drawImage(imageBitmap, 0, 0, canvas.width, canvas.height);
50-
return canvas.toBlob(resolve, format);
51-
});
52-
})
53-
.then(function (resizedImageBlob) {
54-
var result = {
55-
id: ++elem._blazorInputFileNextFileId,
56-
lastModified: originalFile.lastModified,
57-
name: originalFile.name, // Note: we're not changing the file extension
58-
size: resizedImageBlob.size,
59-
type: format,
60-
relativePath: originalFile.relativePath
61-
};
62-
63-
elem._blazorFilesById[result.id] = result;
6439

65-
// Attach the blob data itself as a non-enumerable property so it doesn't appear in the JSON
66-
Object.defineProperty(result, 'blob', { value: resizedImageBlob });
67-
68-
return result;
40+
return new Promise(function (resolve) {
41+
var originalFileImage = new Image();
42+
originalFileImage.onload = function () { resolve(originalFileImage); };
43+
originalFileImage.src = URL.createObjectURL(originalFile.blob);
44+
}).then(function (loadedImage) {
45+
return new Promise(function (resolve) {
46+
var desiredWidthRatio = Math.min(1, maxWidth / loadedImage.width);
47+
var desiredHeightRatio = Math.min(1, maxHeight / loadedImage.height);
48+
var chosenSizeRatio = Math.min(desiredWidthRatio, desiredHeightRatio);
49+
50+
var canvas = document.createElement('canvas');
51+
canvas.width = Math.round(loadedImage.width * chosenSizeRatio);
52+
canvas.height = Math.round(loadedImage.height * chosenSizeRatio);
53+
canvas.getContext('2d').drawImage(loadedImage, 0, 0, canvas.width, canvas.height);
54+
canvas.toBlob(resolve, format);
6955
});
56+
}).then(function (resizedImageBlob) {
57+
var result = {
58+
id: ++elem._blazorInputFileNextFileId,
59+
lastModified: originalFile.lastModified,
60+
name: originalFile.name, // Note: we're not changing the file extension
61+
size: resizedImageBlob.size,
62+
type: format,
63+
relativePath: originalFile.relativePath
64+
};
65+
66+
elem._blazorFilesById[result.id] = result;
67+
68+
// Attach the blob data itself as a non-enumerable property so it doesn't appear in the JSON
69+
Object.defineProperty(result, 'blob', { value: resizedImageBlob });
70+
71+
return result;
72+
});
7073
},
7174

7275
readFileData: function readFileData(elem, fileId, startOffset, count) {

0 commit comments

Comments
 (0)