Skip to content

feature: Use the default quality: 0.9 value across all platforms #137

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

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ ImageEditor.cropImage(uri, cropData).then((url) => {

### `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', 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 | The format of the resulting image, possible values are `jpeg`, `png`, `webp`. <br/> **Default value**: based on the provided image; if value determination is not possible, `jpeg` will be used as a fallback. <br/> `webp` isn't supported by iOS. |
| 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**: `0.9` |
| `format` | No | The format of the resulting image, possible values are `jpeg`, `png`, `webp`. <br/> **Default value**: based on the provided image; if value determination is not possible, `jpeg` will be used as a fallback. <br/> `webp` isn't supported by iOS. |

```ts
cropData: ImageCropData = {
Expand Down
6 changes: 4 additions & 2 deletions ios/RNCImageEditor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#import "RCTImageUtils.h"
#endif

#define DEFAULT_COMPRESSION_QUALITY 0.9

@implementation RNCImageEditor

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -57,7 +59,7 @@ - (void) cropImage:(NSString *)uri
}
NSString *displaySize = data.resizeMode();
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: uri]];
CGFloat compressionQuality = 1;
CGFloat compressionQuality = DEFAULT_COMPRESSION_QUALITY;
if (data.quality().has_value()) {
compressionQuality = *data.quality();
}
Expand All @@ -75,7 +77,7 @@ - (void) cropImage:(NSString *)uri
if(displaySize){
targetSize = [RCTConvert CGSize:cropData[@"displaySize"]];
}
CGFloat compressionQuality = 1;
CGFloat compressionQuality = DEFAULT_COMPRESSION_QUALITY;
if(cropData[@"quality"]){
compressionQuality = [RCTConvert CGFloat:cropData[@"quality"]];
}
Expand Down
4 changes: 3 additions & 1 deletion src/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ function fetchImage(imgSrc: string): Promise<HTMLImageElement> {
});
}

const DEFAULT_COMPRESSION_QUALITY = 0.9;

class ImageEditor {
static cropImage(imgSrc: string, cropData: ImageCropData): Promise<string> {
/**
Expand All @@ -57,7 +59,7 @@ class ImageEditor {
const canvas = drawImage(image, cropData);
return canvas.toDataURL(
`image/${cropData.format ?? 'jpeg'}`,
cropData.quality ?? 1
cropData.quality ?? DEFAULT_COMPRESSION_QUALITY
);
});
}
Expand Down