|
36 | 36 |
|
37 | 37 | toImageFile(elem, fileId, format, maxWidth, maxHeight) { |
38 | 38 | 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; |
64 | 39 |
|
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); |
69 | 55 | }); |
| 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 | + }); |
70 | 73 | }, |
71 | 74 |
|
72 | 75 | readFileData: function readFileData(elem, fileId, startOffset, count) { |
|
0 commit comments