Skip to content

Commit ad8bdad

Browse files
committed
refactor: remove autoCompress Prop and add compressionMethod prop
1 parent 818c7eb commit ad8bdad

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ react-native link react-native-compressor
5858

5959
### Image
6060

61-
##### For Whatsapp Image Compression
61+
##### For Like Whatsapp Image Compression
6262

6363
```js
6464
import { Image } from 'react-native-compressor';
6565

6666
const result = await Image.compress('file://path_of_file/image.jpg', {
67-
autoCompress: true,
67+
compressionMethod: 'auto',
6868
});
6969
```
7070

@@ -122,19 +122,19 @@ const result = await Video.compress(
122122

123123
### CompressorOptions
124124

125-
- ###### `autoCompress: boolean` (default: false)
125+
- ###### `compressionMethod: compressionMethod` (default: "manual")
126126

127-
if you want to compress images like whatsapp then make this prop `true`. by enable this option other option will not effect in compression
127+
if you want to compress images like **whatsapp** then make this prop `auto`. Can be either `manual` or `auto`, defines the Compression Method.
128128

129-
- ###### `maxWidth: number` (default: 1024)
129+
- ###### `maxWidth: number` (default: 1280)
130130

131131
The maximum width boundary used as the main boundary in resizing a landscape image.
132132

133-
- ###### `maxHeight: number` (default: 1024)
133+
- ###### `maxHeight: number` (default: 1280)
134134

135135
The maximum height boundary used as the main boundary in resizing a portrait image.
136136

137-
- ###### `quality: number` (default: 1.0)
137+
- ###### `quality: number` (default: 0.8)
138138

139139
The quality modifier for the `JPEG` file format, can be specified when output is `PNG` but will be ignored.
140140

android/src/main/java/com/reactnativecompressor/CompressorModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public void image_compress(
6161
try {
6262
final ImageCompressorOptions options = ImageCompressorOptions.fromMap(optionMap);
6363

64-
if(options.autoCompress)
64+
if(options.compressionMethod==ImageCompressorOptions.CompressionMethod.auto)
6565
{
66-
String returnableResult=ImageCompressor.autoCompressImage(imagePath,options.output.toString(),reactContext);
66+
String returnableResult=ImageCompressor.autoCompressImage(imagePath,options,reactContext);
6767
promise.resolve(returnableResult);
6868
}
6969
else

android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525

2626
public class ImageCompressor {
27-
private static final float autoCompressMaxHeight = 1280.0f;
28-
private static final float audoCompressMaxWidth = 1280.0f;
27+
2928

3029
public static String getRNFileUrl(String filePath) {
3130
File returnAbleFile= new File(filePath);
@@ -131,7 +130,11 @@ public static String manualCompressImage(String imagePath,ImageCompressorOptions
131130
}
132131

133132

134-
public static String autoCompressImage(String imagePath,String outputExtension, ReactApplicationContext reactContext) {
133+
public static String autoCompressImage(String imagePath,ImageCompressorOptions compressorOptions, ReactApplicationContext reactContext) {
134+
String outputExtension=compressorOptions.output.toString();
135+
int quality= (int) (compressorOptions.quality*100);
136+
float autoCompressMaxHeight = compressorOptions.maxHeight;
137+
float audoCompressMaxWidth = compressorOptions.maxWidth;
135138

136139
Uri uri= Uri.parse(imagePath);
137140
imagePath = uri.getPath();
@@ -221,7 +224,7 @@ public static String autoCompressImage(String imagePath,String outputExtension,
221224
out = new FileOutputStream(filepath);
222225

223226
//write the compressed bitmap at the destination specified by filename.
224-
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
227+
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out);
225228

226229
} catch (FileNotFoundException e) {
227230
e.printStackTrace();

android/src/main/java/com/reactnativecompressor/Image/utils/ImageCompressorOptions.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public static ImageCompressorOptions fromMap(ReadableMap map) {
1212
final String key = iterator.nextKey();
1313

1414
switch (key) {
15-
case "autoCompress":
16-
options.autoCompress = map.getBoolean(key);
15+
case "compressionMethod":
16+
options.compressionMethod = CompressionMethod.valueOf(map.getString(key));
1717
break;
1818
case "maxWidth":
1919
options.maxWidth = map.getInt(key);
@@ -51,10 +51,14 @@ public enum ReturnableOutputType {
5151
base64, uri
5252
}
5353

54-
public boolean autoCompress = false;
55-
public int maxWidth = 640;
56-
public int maxHeight = 480;
57-
public float quality = 1.0f;
54+
public enum CompressionMethod {
55+
auto, manual
56+
}
57+
58+
public CompressionMethod compressionMethod = CompressionMethod.manual;
59+
public int maxWidth = 1280;
60+
public int maxHeight = 1280;
61+
public float quality = 0.8f;
5862
public InputType input = InputType.uri;
5963
public OutputType output = OutputType.jpg;
6064
public ReturnableOutputType returnableOutputType = ReturnableOutputType.uri;

example/src/Screens/Image/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const Index = () => {
4040
}
4141

4242
Image.compress(source.uri, {
43-
autoCompress: true,
43+
compressionMethod: 'auto',
4444
})
4545
.then(async (compressedFileUri) => {
4646
setCommpressedUri(compressedFileUri);

src/Image/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ export type OutputType = 'jpg' | 'png';
77

88
export type ReturnableOutputType = 'uri' | 'base64';
99

10+
export type compressionMethod = 'auto' | 'manual';
11+
1012
export type CompressorOptions = {
1113
/***
1214
* The maximum width boundary used when compressing a landscape image.
1315
*/
14-
autoCompress?: boolean;
16+
compressionMethod?: compressionMethod;
1517
/***
1618
* The maximum width boundary used when compressing a landscape image.
1719
*/

0 commit comments

Comments
 (0)