Add outputPath to options (#16)
* Add outputPath for android * Add outputPath for ios * Update readme * Move outputPath to the end * Update readme
This commit is contained in:
parent
ac2a1728f0
commit
af9f77d965
20
README.md
20
README.md
|
@ -103,7 +103,7 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
|
||||||
```javascript
|
```javascript
|
||||||
import ImageResizer from 'react-native-image-resizer';
|
import ImageResizer from 'react-native-image-resizer';
|
||||||
|
|
||||||
ImageResizer.createResizedImage(imageUri, newWidth, newHeight, compressFormat, quality).then((resizedImageUri) => {
|
ImageResizer.createResizedImage(imageUri, newWidth, newHeight, compressFormat, quality, rotation, outputPath).then((resizedImageUri) => {
|
||||||
// resizeImageUri is the URI of the new image that can now be displayed, uploaded...
|
// resizeImageUri is the URI of the new image that can now be displayed, uploaded...
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
// Oops, something went wrong. Check that the filename is correct and
|
// Oops, something went wrong. Check that the filename is correct and
|
||||||
|
@ -117,12 +117,16 @@ A basic, sample app is available in [the `example` folder](https://github.com/ba
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### `promise createResizedImage(path, maxWidth, maxHeight, compressFormat, quality, rotation = 0)`
|
### `promise createResizedImage(path, maxWidth, maxHeight, compressFormat, quality, rotation = 0, outputPath)`
|
||||||
|
|
||||||
Open the image at the given path and resize it so that it is less than the specified `maxWidth` and `maxHeight` (i.e: ratio is preserved). `compressFormat` is either `JPEG`, `PNG` (android only) or `WEBP` (android only).
|
|
||||||
|
|
||||||
`quality` is a number between 0 and 100, used for the JPEG compression.
|
|
||||||
|
|
||||||
`rotation` is the rotation to apply to the image, in degrees, for android only. On iOS, the resizing is done such that the orientation is always up.
|
|
||||||
|
|
||||||
The promise resolves with a string containing the uri of the new file.
|
The promise resolves with a string containing the uri of the new file.
|
||||||
|
|
||||||
|
Option | Description
|
||||||
|
------ | -----------
|
||||||
|
path | Path of image
|
||||||
|
maxWidth | Image max width (ratio is preserved)
|
||||||
|
maxHeight | Image max height (ratio is preserved)
|
||||||
|
compressFormat | Can be either JPEG, PNG (android only) or WEBP (android only).
|
||||||
|
quality | A number between 0 and 100. Used for the JPEG compression.
|
||||||
|
rotation | Rotation to apply to the image, in degrees, for android only. On iOS, the resizing is done such that the orientation is always up.
|
||||||
|
outputPath | The resized image path. If null, resized image will be stored in cache folder. To set outputPath make sure to add option for rotation too (if no rotation is needed, just set it to 0).
|
||||||
|
|
|
@ -104,10 +104,16 @@ class ImageResizer {
|
||||||
|
|
||||||
public static String createResizedImage(Context context, String imagePath, int newWidth,
|
public static String createResizedImage(Context context, String imagePath, int newWidth,
|
||||||
int newHeight, Bitmap.CompressFormat compressFormat,
|
int newHeight, Bitmap.CompressFormat compressFormat,
|
||||||
int quality, int rotation) throws IOException {
|
int quality, int rotation, String outputPath) throws IOException {
|
||||||
|
|
||||||
Bitmap resizedImage = ImageResizer.rotateImage(ImageResizer.resizeImage(imagePath, newWidth, newHeight,context), rotation);
|
Bitmap resizedImage = ImageResizer.rotateImage(ImageResizer.resizeImage(imagePath, newWidth, newHeight, context), rotation);
|
||||||
return ImageResizer.saveImage(resizedImage, context.getCacheDir(),
|
|
||||||
|
File path = context.getCacheDir();
|
||||||
|
if (outputPath != null || !outputPath.isEmpty()) {
|
||||||
|
path = new File(outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ImageResizer.saveImage(resizedImage, path,
|
||||||
Long.toString(new Date().getTime()), compressFormat, quality);
|
Long.toString(new Date().getTime()), compressFormat, quality);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,22 +32,22 @@ class ImageResizerModule extends ReactContextBaseJavaModule {
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void createResizedImage(String imagePath, int newWidth, int newHeight, String compressFormat,
|
public void createResizedImage(String imagePath, int newWidth, int newHeight, String compressFormat,
|
||||||
int quality, int rotation, final Callback successCb, final Callback failureCb) {
|
int quality, int rotation, String outputPath, final Callback successCb, final Callback failureCb) {
|
||||||
try {
|
try {
|
||||||
createResizedImageWithExceptions(imagePath, newWidth, newHeight, compressFormat, quality,
|
createResizedImageWithExceptions(imagePath, newWidth, newHeight, compressFormat, quality,
|
||||||
rotation, successCb, failureCb);
|
rotation, outputPath, successCb, failureCb);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
failureCb.invoke(e.getMessage());
|
failureCb.invoke(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createResizedImageWithExceptions(String imagePath, int newWidth, int newHeight,
|
private void createResizedImageWithExceptions(String imagePath, int newWidth, int newHeight,
|
||||||
String compressFormatString, int quality, int rotation,
|
String compressFormatString, int quality, int rotation, String outputPath,
|
||||||
final Callback successCb, final Callback failureCb) throws IOException {
|
final Callback successCb, final Callback failureCb) throws IOException {
|
||||||
Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.valueOf(compressFormatString);
|
Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.valueOf(compressFormatString);
|
||||||
imagePath = imagePath.replace("file:", "");
|
imagePath = imagePath.replace("file:", "");
|
||||||
String resizedImagePath = ImageResizer.createResizedImage(this.context, imagePath, newWidth,
|
String resizedImagePath = ImageResizer.createResizedImage(this.context, imagePath, newWidth,
|
||||||
newHeight, compressFormat, quality, rotation);
|
newHeight, compressFormat, quality, rotation, outputPath);
|
||||||
|
|
||||||
successCb.invoke("file:" + resizedImagePath);
|
successCb.invoke("file:" + resizedImagePath);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ import React from 'react-native';
|
||||||
const ImageResizerAndroid = React.NativeModules.ImageResizerAndroid;
|
const ImageResizerAndroid = React.NativeModules.ImageResizerAndroid;
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
createResizedImage: (imagePath, newWidth, newHeight, compressFormat, quality, rotation = 0) => {
|
createResizedImage: (imagePath, newWidth, newHeight, compressFormat, quality, rotation = 0, outputPath) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
ImageResizerAndroid.createResizedImage(imagePath, newWidth, newHeight,
|
ImageResizerAndroid.createResizedImage(imagePath, newWidth, newHeight,
|
||||||
compressFormat, quality, rotation, resolve, reject);
|
compressFormat, quality, rotation, outputPath, resolve, reject);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,13 +3,13 @@ import {
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
createResizedImage: (path, width, height, format, quality) => {
|
createResizedImage: (path, width, height, format, quality, rotation = 0, outputPath) => {
|
||||||
if (format !== 'JPEG') {
|
if (format !== 'JPEG') {
|
||||||
throw new Error('Only JPEG format is supported by createResizedImage');
|
throw new Error('Only JPEG format is supported by createResizedImage');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
NativeModules.ImageResizer.createResizedImage(path, width, height, quality, (err, resizedPath) => {
|
NativeModules.ImageResizer.createResizedImage(path, width, height, quality, outputPath, (err, resizedPath) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,13 +22,20 @@ void saveImage(NSString * fullPath, UIImage * image, float quality)
|
||||||
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
|
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString * generateCacheFilePath(NSString * ext)
|
NSString * generateFilePath(NSString * ext, NSString * outputPath)
|
||||||
{
|
{
|
||||||
|
NSString* directory;
|
||||||
|
|
||||||
|
if ([outputPath length] == 0) {
|
||||||
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
||||||
NSString* cacheDirectory = [paths firstObject];
|
directory = [paths firstObject];
|
||||||
|
} else {
|
||||||
|
directory = outputPath;
|
||||||
|
}
|
||||||
|
|
||||||
NSString* name = [[NSUUID UUID] UUIDString];
|
NSString* name = [[NSUUID UUID] UUIDString];
|
||||||
NSString* fullName = [NSString stringWithFormat:@"%@.%@", name, ext];
|
NSString* fullName = [NSString stringWithFormat:@"%@.%@", name, ext];
|
||||||
NSString* fullPath = [cacheDirectory stringByAppendingPathComponent:fullName];
|
NSString* fullPath = [directory stringByAppendingPathComponent:fullName];
|
||||||
|
|
||||||
return fullPath;
|
return fullPath;
|
||||||
}
|
}
|
||||||
|
@ -37,10 +44,11 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
width:(float)width
|
width:(float)width
|
||||||
height:(float)height
|
height:(float)height
|
||||||
quality:(float)quality
|
quality:(float)quality
|
||||||
|
outputPath:(NSString *)outputPath
|
||||||
callback:(RCTResponseSenderBlock)callback)
|
callback:(RCTResponseSenderBlock)callback)
|
||||||
{
|
{
|
||||||
CGSize newSize = CGSizeMake(width, height);
|
CGSize newSize = CGSizeMake(width, height);
|
||||||
NSString* fullPath = generateCacheFilePath(@"jpg");
|
NSString* fullPath = generateFilePath(@"jpg", outputPath);
|
||||||
|
|
||||||
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
|
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
|
||||||
if (error || image == nil) {
|
if (error || image == nil) {
|
||||||
|
|
Loading…
Reference in New Issue