-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When uploading, automatically reduce the size of receipt image #45448
Merged
luacmartins
merged 16 commits into
Expensify:main
from
nkdengineer:fix/reduce-size-image-44084
Aug 5, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a557e6f
fix: reduce size image
nkdengineer 6ddcc58
fix: add resize in native
nkdengineer 01815b2
add solution for native
nkdengineer e8442f2
merge main
nkdengineer a64a32d
add optional
nkdengineer 0176f01
add comment
nkdengineer 21a4cb5
Update src/libs/fileDownload/FileUtils.ts
nkdengineer 60b6b57
Update src/libs/fileDownload/FileUtils.ts
nkdengineer 6930ad4
Merge branch 'main' into fix/reduce-size-image-44084
nkdengineer b5244f6
add warning about exceeded dimension
nkdengineer c63cdc1
temporary disable validate for test
nkdengineer 8ca9b74
Merge branch 'main' into fix/reduce-size-image-44084
nkdengineer fbf01ba
add patch resize
nkdengineer 10faa93
revert unnecessary change
nkdengineer 623fbe8
update patch
nkdengineer b8b8eea
fix error
nkdengineer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luacmartins This is not true. I need 24MB file to check this flow. See this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you want to manipulate this condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shubham1206agra you can download the large image here