-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[New architecture] Replace @oguzhnatly/react-native-image-manipulator
with expo-image-manipulator
#36019
Merged
roryabraham
merged 15 commits into
Expensify:main
from
software-mansion-labs:add-expo-image-manipulator
Mar 5, 2024
Merged
[New architecture] Replace @oguzhnatly/react-native-image-manipulator
with expo-image-manipulator
#36019
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
286e234
Migrate to expo image manipulator
kowczarz d10d09d
Remove unused dependency
kowczarz 21cf30e
Fixes for fabric
kowczarz 8dbde7d
Merge remote-tracking branch 'expensify/main' into add-expo-image-man…
kowczarz f8a4f81
Cleanup Podfile.lock
kowczarz 63d39c4
Fix TS issues
kowczarz 1682cdb
Code review changes
kowczarz 0e48707
Code review change
kowczarz d40d7e3
Merge branch 'main' into add-expo-image-manipulator
kosmydel caf4552
fix imports
kosmydel bb666cc
fix podfile
kosmydel 8618c30
add removed line
kosmydel 8e57fb5
fix package.json
kosmydel f1860cd
podfile
kosmydel a070e12
Merge branch 'Expensify:main' into add-expo-image-manipulator
kosmydel 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
19 changes: 0 additions & 19 deletions
19
patches/@oguzhnatly+react-native-image-manipulator+1.0.5.patch
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import {SaveFormat} from 'expo-image-manipulator'; | ||
import CONST from '@src/CONST'; | ||
|
||
function getSaveFormat(type: string) { | ||
switch (type) { | ||
case CONST.IMAGE_FILE_FORMAT.PNG: | ||
return SaveFormat.PNG; | ||
case CONST.IMAGE_FILE_FORMAT.WEBP: | ||
return SaveFormat.WEBP; | ||
case CONST.IMAGE_FILE_FORMAT.JPEG: | ||
return SaveFormat.JPEG; | ||
default: | ||
return SaveFormat.JPEG; | ||
} | ||
} | ||
|
||
export default getSaveFormat; |
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 |
---|---|---|
@@ -1,127 +1,19 @@ | ||
import type {CropOptions, CropOrRotateImage, CropOrRotateImageOptions} from './types'; | ||
|
||
type SizeFromAngle = { | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
/** | ||
* Calculates a size of canvas after rotation | ||
*/ | ||
function sizeFromAngle(width: number, height: number, angle: number): SizeFromAngle { | ||
const radians = (angle * Math.PI) / 180; | ||
let sine = Math.cos(radians); | ||
let cosine = Math.sin(radians); | ||
if (cosine < 0) { | ||
cosine = -cosine; | ||
} | ||
if (sine < 0) { | ||
sine = -sine; | ||
} | ||
return {width: height * cosine + width * sine, height: height * sine + width * cosine}; | ||
} | ||
|
||
/** | ||
* Creates a new rotated canvas | ||
*/ | ||
function rotateCanvas(canvas: HTMLCanvasElement, degrees: number): HTMLCanvasElement { | ||
const {width, height} = sizeFromAngle(canvas.width, canvas.height, degrees); | ||
|
||
// We have to create a new canvas because it is not possible to change already drawn | ||
// elements. Transformations such as rotation have to be applied before drawing | ||
const result = document.createElement('canvas'); | ||
result.width = width; | ||
result.height = height; | ||
|
||
const context = result.getContext('2d'); | ||
if (context) { | ||
// In order to rotate image along its center we have to apply next transformation | ||
context.translate(result.width / 2, result.height / 2); | ||
|
||
const radians = (degrees * Math.PI) / 180; | ||
context.rotate(radians); | ||
|
||
context.drawImage(canvas, -canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Creates new cropped canvas and returns it | ||
*/ | ||
function cropCanvas(canvas: HTMLCanvasElement, options: CropOptions) { | ||
let {originX = 0, originY = 0, width = 0, height = 0} = options; | ||
const clamp = (value: number, max: number) => Math.max(0, Math.min(max, value)); | ||
|
||
width = clamp(width, canvas.width); | ||
height = clamp(height, canvas.height); | ||
originX = clamp(originX, canvas.width); | ||
originY = clamp(originY, canvas.height); | ||
|
||
width = Math.min(originX + width, canvas.width) - originX; | ||
height = Math.min(originY + height, canvas.height) - originY; | ||
|
||
const result = document.createElement('canvas'); | ||
result.width = width; | ||
result.height = height; | ||
|
||
const context = result.getContext('2d'); | ||
context?.drawImage(canvas, originX, originY, width, height, 0, 0, width, height); | ||
|
||
return result; | ||
} | ||
|
||
function convertCanvasToFile(canvas: HTMLCanvasElement, options: CropOrRotateImageOptions): Promise<File> { | ||
return new Promise((resolve) => { | ||
canvas.toBlob((blob) => { | ||
if (!blob) { | ||
return; | ||
} | ||
const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'}); | ||
file.uri = URL.createObjectURL(file); | ||
resolve(file); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Loads image from specified url | ||
*/ | ||
function loadImageAsync(uri: string): Promise<HTMLCanvasElement> { | ||
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 context = canvas.getContext('2d'); | ||
context?.drawImage(imageSource, 0, 0, imageSource.naturalWidth, imageSource.naturalHeight); | ||
|
||
resolve(canvas); | ||
}; | ||
imageSource.onerror = () => reject(canvas); | ||
imageSource.src = uri; | ||
}); | ||
} | ||
|
||
/** | ||
* Crops and rotates the image on web | ||
*/ | ||
import {manipulateAsync} from 'expo-image-manipulator'; | ||
import getSaveFormat from './getSaveFormat'; | ||
import type {CropOrRotateImage} from './types'; | ||
|
||
const cropOrRotateImage: CropOrRotateImage = (uri, actions, options) => | ||
loadImageAsync(uri).then((originalCanvas) => { | ||
const resultCanvas = actions.reduce((canvas, action) => { | ||
if (action.crop) { | ||
return cropCanvas(canvas, action.crop); | ||
} | ||
if (action.rotate) { | ||
return rotateCanvas(canvas, action.rotate); | ||
} | ||
return canvas; | ||
}, originalCanvas); | ||
return convertCanvasToFile(resultCanvas, options); | ||
new Promise((resolve) => { | ||
const format = getSaveFormat(options.type); | ||
manipulateAsync(uri, actions, {compress: options.compress, format}).then((result) => { | ||
fetch(result.uri) | ||
.then((res) => res.blob()) | ||
.then((blob) => { | ||
const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'}); | ||
file.uri = URL.createObjectURL(file); | ||
resolve(file); | ||
}); | ||
}); | ||
}); | ||
|
||
export default cropOrRotateImage; |
Oops, something went wrong.
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.
I am wondering in which case will
options.name
will not be set and we'll have to fallback forfileName.jpeg
? Can we use any other fallback? instead of hardcoding?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.
Same here, I didn't want to change the old logic when it wasn't necessary https://github.com/Expensify/App/pull/36019/files#diff-fff63959f3cf9ee8d503ba4eeadac6e6b04a65d4fcc2a0e38f52e86b9f2f1663L80