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

Commit

Permalink
For Safari support, avoid use of createImageBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Apr 28, 2020
1 parent db43dc1 commit 2bb5968
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions BlazorInputFile/wwwroot/inputfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,40 @@

toImageFile(elem, fileId, format, maxWidth, maxHeight) {
var originalFile = getFileById(elem, fileId);
return createImageBitmap(originalFile.blob)
.then(function (imageBitmap) {
return new Promise(function (resolve) {
var desiredWidthRatio = Math.min(1, maxWidth / imageBitmap.width);
var desiredHeightRatio = Math.min(1, maxHeight / imageBitmap.height);
var chosenSizeRatio = Math.min(desiredWidthRatio, desiredHeightRatio);

var canvas = document.createElement('canvas');
canvas.width = Math.round(imageBitmap.width * chosenSizeRatio);
canvas.height = Math.round(imageBitmap.height * chosenSizeRatio);
canvas.getContext('2d').drawImage(imageBitmap, 0, 0, canvas.width, canvas.height);
return canvas.toBlob(resolve, format);
});
})
.then(function (resizedImageBlob) {
var result = {
id: ++elem._blazorInputFileNextFileId,
lastModified: originalFile.lastModified,
name: originalFile.name, // Note: we're not changing the file extension
size: resizedImageBlob.size,
type: format,
relativePath: originalFile.relativePath
};

elem._blazorFilesById[result.id] = result;

// Attach the blob data itself as a non-enumerable property so it doesn't appear in the JSON
Object.defineProperty(result, 'blob', { value: resizedImageBlob });

return result;
return new Promise(function (resolve) {
var originalFileImage = new Image();
originalFileImage.onload = function () { resolve(originalFileImage); };
originalFileImage.src = URL.createObjectURL(originalFile.blob);
}).then(function (loadedImage) {
return new Promise(function (resolve) {
var desiredWidthRatio = Math.min(1, maxWidth / loadedImage.width);
var desiredHeightRatio = Math.min(1, maxHeight / loadedImage.height);
var chosenSizeRatio = Math.min(desiredWidthRatio, desiredHeightRatio);

var canvas = document.createElement('canvas');
canvas.width = Math.round(loadedImage.width * chosenSizeRatio);
canvas.height = Math.round(loadedImage.height * chosenSizeRatio);
canvas.getContext('2d').drawImage(loadedImage, 0, 0, canvas.width, canvas.height);
canvas.toBlob(resolve, format);
});
}).then(function (resizedImageBlob) {
var result = {
id: ++elem._blazorInputFileNextFileId,
lastModified: originalFile.lastModified,
name: originalFile.name, // Note: we're not changing the file extension
size: resizedImageBlob.size,
type: format,
relativePath: originalFile.relativePath
};

elem._blazorFilesById[result.id] = result;

// Attach the blob data itself as a non-enumerable property so it doesn't appear in the JSON
Object.defineProperty(result, 'blob', { value: resizedImageBlob });

return result;
});
},

readFileData: function readFileData(elem, fileId, startOffset, count) {
Expand Down

0 comments on commit 2bb5968

Please sign in to comment.