-
Notifications
You must be signed in to change notification settings - Fork 120
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
feature: Add web support #131
Merged
+110
−12
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -29,7 +29,7 @@ npx pod-install | |
Start by importing the library: | ||
|
||
```ts | ||
import ImageEditor from "@react-native-community/image-editor"; | ||
import ImageEditor from '@react-native-community/image-editor'; | ||
``` | ||
|
||
### Crop image | ||
|
@@ -39,33 +39,38 @@ Crop the image specified by the URI param. If URI points to a remote image, it w | |
If the cropping process is successful, the resultant cropped image will be stored in the cache path, and the URI returned in the promise will point to the image in the cache path. Remember to delete the cropped image from the cache path when you are done with it. | ||
|
||
```ts | ||
ImageEditor.cropImage(uri, cropData).then(url => { | ||
console.log("Cropped image uri", url); | ||
}) | ||
ImageEditor.cropImage(uri, cropData).then((url) => { | ||
console.log('Cropped image uri', url); | ||
// In case of Web, the `url` is the base64 string | ||
}); | ||
``` | ||
|
||
### `cropData: ImageCropData` | ||
| Property | Required | Description | | ||
|---------------|----------|----------------------------------------------------------------------------------------------------------------------------| | ||
| `offset` | Yes | The top-left corner of the cropped image, specified in the original image's coordinate space | | ||
| `size` | Yes | Size (dimensions) of the cropped image | | ||
| `displaySize` | No | Size to which you want to scale the cropped image | | ||
| `resizeMode` | No | Resizing mode to use when scaling the image (iOS only, android resize mode is always 'cover') **Default value**: 'contain' | | ||
| `quality` | No | The quality of the resulting image, expressed as a value from `0.0` to `1.0`. <br/>The value `0.0` represents the maximum compression (or lowest quality) while the value `1.0` represents the least compression (or best quality).<br/>iOS supports only `JPEG` format, while Android supports both `JPEG`, `WEBP` and `PNG` formats.<br/>**Default value**: (iOS: `1`), (Android: `0.9`) | | ||
|
||
| Property | Required | Description | | ||
| ------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `offset` | Yes | The top-left corner of the cropped image, specified in the original image's coordinate space | | ||
| `size` | Yes | Size (dimensions) of the cropped image | | ||
| `displaySize` | No | Size to which you want to scale the cropped image | | ||
| `resizeMode` | No | Resizing mode to use when scaling the image (iOS only, Android resize mode is always 'cover', Web - no support) **Default value**: 'contain' | | ||
| `quality` | No | The quality of the resulting image, expressed as a value from `0.0` to `1.0`. <br/>The value `0.0` represents the maximum compression (or lowest quality) while the value `1.0` represents the least compression (or best quality).<br/>iOS supports only `JPEG` format, while Android/Web supports both `JPEG`, `WEBP` and `PNG` formats.<br/>**Default value**: (iOS: `1`), (Android: `0.9`) | | ||
| `format` | No | **(WEB ONLY)** The format of the resulting image, possible values are `jpeg`, `png`, `webp`, **Default value**: `jpeg` | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be cool to support it in Android and iOS implementation as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, I going to add it after fixing one important issue on Android |
||
|
||
```ts | ||
cropData: ImageCropData = { | ||
offset: {x: number, y: number}, | ||
size: {width: number, height: number}, | ||
displaySize: {width: number, height: number}, | ||
resizeMode: 'contain' | 'cover' | 'stretch', | ||
quality: number // 0...1 | ||
quality: number, // 0...1 | ||
format: 'jpeg' | 'png' | 'webp' // web only | ||
}; | ||
``` | ||
|
||
For more advanced usage check our [example app](/example/src/App.tsx). | ||
|
||
<!-- badges --> | ||
|
||
[build-badge]: https://github.com/callstack/react-native-image-editor/actions/workflows/main.yml/badge.svg | ||
[build]: https://github.com/callstack/react-native-image-editor/actions/workflows/main.yml | ||
[version-badge]: https://img.shields.io/npm/v/@react-native-community/image-editor.svg | ||
|
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,76 @@ | ||
import type { Spec } from './NativeRNCImageEditor'; | ||
|
||
type ImageCropDataFromSpec = Parameters<Spec['cropImage']>[1]; | ||
|
||
export interface ImageCropData | ||
extends Omit<ImageCropDataFromSpec, 'resizeMode'> { | ||
resizeMode?: 'contain' | 'cover' | 'stretch'; | ||
// ^^^ codegen doesn't support union types yet | ||
// so to provide more type safety we override the type here | ||
format?: 'png' | 'jpeg' | 'webp'; // web only | ||
} | ||
|
||
function drawImage( | ||
img: HTMLImageElement, | ||
{ offset, size, displaySize }: ImageCropData | ||
): HTMLCanvasElement { | ||
const canvas = document.createElement('canvas'); | ||
const context = canvas.getContext('2d'); | ||
|
||
if (!context) { | ||
throw new Error('Failed to get canvas context'); | ||
} | ||
|
||
const sx = offset.x, | ||
sy = offset.y, | ||
sWidth = size.width, | ||
sHeight = size.height, | ||
dx = 0, | ||
dy = 0, | ||
dWidth = displaySize?.width ?? sWidth, | ||
dHeight = displaySize?.height ?? sHeight; | ||
|
||
canvas.width = dWidth; | ||
canvas.height = dHeight; | ||
|
||
context.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); | ||
|
||
return canvas; | ||
} | ||
|
||
function fetchImage(imgSrc: string): Promise<HTMLImageElement> { | ||
return new Promise<HTMLImageElement>((resolve, reject) => { | ||
const onceOptions = { once: true }; | ||
const img = new Image(); | ||
|
||
function onImageError(event: ErrorEvent) { | ||
reject(event); | ||
} | ||
|
||
function onLoad() { | ||
resolve(img); | ||
} | ||
|
||
img.addEventListener('error', onImageError, onceOptions); | ||
img.addEventListener('load', onLoad, onceOptions); | ||
img.crossOrigin = 'anonymous'; | ||
img.src = imgSrc; | ||
}); | ||
} | ||
|
||
class ImageEditor { | ||
static cropImage(imgSrc: string, cropData: ImageCropData): Promise<string> { | ||
/** | ||
* Returns a promise that resolves with the base64 encoded string of the cropped image | ||
*/ | ||
return fetchImage(imgSrc).then(function onfulfilledImgToCanvas(image) { | ||
const canvas = drawImage(image, cropData); | ||
return canvas.toDataURL( | ||
`image/${cropData.format ?? 'jpeg'}`, | ||
cropData.quality ?? 1 | ||
); | ||
}); | ||
} | ||
} | ||
|
||
export default ImageEditor; |
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.
You can update that iOS supports
PNG
as wellThere 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.
PNG uses lossless compression, that's why
UIImagePNGRepresentation
does not acceptcompressionQuality
parameter likeUIImageJPEGRepresentation
does, see:react-native-image-editor/ios/RNCImageEditor.mm
Lines 113 to 121 in 47a2c57