forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#45448 from nkdengineer/fix/reduce-size-i…
…mage-44084
- Loading branch information
Showing
8 changed files
with
163 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
diff --git a/node_modules/expo-image-manipulator/build/ExpoImageManipulator.web.js b/node_modules/expo-image-manipulator/build/ExpoImageManipulator.web.js | ||
index 5b77ad6..a3ecdb0 100644 | ||
--- a/node_modules/expo-image-manipulator/build/ExpoImageManipulator.web.js | ||
+++ b/node_modules/expo-image-manipulator/build/ExpoImageManipulator.web.js | ||
@@ -1,5 +1,13 @@ | ||
import { crop, extent, flip, resize, rotate } from './actions/index.web'; | ||
import { getContext } from './utils/getContext.web'; | ||
+ | ||
+const SAFARI_MOBILE_CANVAS_LIMIT = 4096; | ||
+ | ||
+const isMobileIOS = () => { | ||
+ const userAgent = navigator.userAgent; | ||
+ return /iP(ad|od|hone)/i.test(userAgent) && /(WebKit|CriOS|FxiOS|OPiOS|mercury)/i.test(userAgent); | ||
+}; | ||
+ | ||
function getResults(canvas, options) { | ||
let uri; | ||
if (options) { | ||
@@ -21,16 +29,49 @@ function getResults(canvas, options) { | ||
base64: uri.replace(/^data:image\/\w+;base64,/, ''), | ||
}; | ||
} | ||
+ | ||
+function getAdjustedCanvasSize(originalWidth, originalHeight) { | ||
+ if(!isMobileIOS()) return { width: originalWidth, height: originalHeight }; | ||
+ | ||
+ const aspectRatio = originalWidth / originalHeight; | ||
+ let newWidth; | ||
+ let newHeight; | ||
+ | ||
+ if (originalWidth <= SAFARI_MOBILE_CANVAS_LIMIT && originalHeight <= SAFARI_MOBILE_CANVAS_LIMIT) { | ||
+ return { width: originalWidth, height: originalHeight }; | ||
+ } | ||
+ | ||
+ if (aspectRatio > 1) { | ||
+ newWidth = SAFARI_MOBILE_CANVAS_LIMIT; | ||
+ newHeight = Math.round(newWidth / aspectRatio); | ||
+ } else { | ||
+ newHeight = SAFARI_MOBILE_CANVAS_LIMIT; | ||
+ newWidth = Math.round(newHeight * aspectRatio); | ||
+ } | ||
+ | ||
+ if (newWidth > SAFARI_MOBILE_CANVAS_LIMIT) { | ||
+ newWidth = SAFARI_MOBILE_CANVAS_LIMIT; | ||
+ newHeight = Math.round(newWidth / aspectRatio); | ||
+ } else if (newHeight > SAFARI_MOBILE_CANVAS_LIMIT) { | ||
+ newHeight = SAFARI_MOBILE_CANVAS_LIMIT; | ||
+ newWidth = Math.round(newHeight * aspectRatio); | ||
+ } | ||
+ | ||
+ return { width: newWidth, height: newHeight }; | ||
+} | ||
+ | ||
function loadImageAsync(uri) { | ||
return new Promise((resolve, reject) => { | ||
const imageSource = new Image(); | ||
imageSource.crossOrigin = 'anonymous'; | ||
const canvas = document.createElement('canvas'); | ||
imageSource.onload = () => { | ||
- canvas.width = imageSource.naturalWidth; | ||
- canvas.height = imageSource.naturalHeight; | ||
+ const adjudstedCanvasSize = getAdjustedCanvasSize(imageSource.naturalWidth, imageSource.naturalHeight); | ||
+ | ||
+ canvas.width = adjudstedCanvasSize.width; | ||
+ canvas.height = adjudstedCanvasSize.height; | ||
const context = getContext(canvas); | ||
- context.drawImage(imageSource, 0, 0, imageSource.naturalWidth, imageSource.naturalHeight); | ||
+ context.drawImage(imageSource, 0, 0, adjudstedCanvasSize.width, adjudstedCanvasSize.height); | ||
resolve(canvas); | ||
}; | ||
imageSource.onerror = () => reject(canvas); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {manipulateAsync} from 'expo-image-manipulator'; | ||
import type {FileObject} from '@components/AttachmentModal'; | ||
import type ImageManipulatorConfig from './type'; | ||
|
||
export default function getImageManipulator({fileUri, width, height, type, fileName}: ImageManipulatorConfig): Promise<FileObject> { | ||
return manipulateAsync(fileUri ?? '', [{resize: {width, height}}]).then((result) => ({ | ||
uri: result.uri, | ||
width: result.width, | ||
height: result.height, | ||
type, | ||
name: fileName, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {manipulateAsync} from 'expo-image-manipulator'; | ||
import type ImageManipulatorConfig from './type'; | ||
|
||
export default function getImageManipulator({fileUri, width, height, fileName}: ImageManipulatorConfig): Promise<File> { | ||
return manipulateAsync(fileUri ?? '', [{resize: {width, height}}]).then((result) => | ||
fetch(result.uri) | ||
.then((res) => res.blob()) | ||
.then((blob) => { | ||
const resizedFile = new File([blob], `${fileName}.jpeg`, {type: 'image/jpeg'}); | ||
resizedFile.uri = URL.createObjectURL(resizedFile); | ||
return resizedFile; | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type ImageManipulatorConfig = { | ||
fileUri: string; | ||
fileName: string; | ||
width: number; | ||
height: number; | ||
type?: string; | ||
}; | ||
|
||
export default ImageManipulatorConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters