diff --git a/README.md b/README.md index 9eba5fe..3b79ef3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## Install ``` -$ cordova plugin add --save cordova-plugin-crop +$ cordova plugin add --save https://github.com/rastafan/cordova-plugin-crop ``` @@ -47,6 +47,10 @@ The resulting JPEG picture width. default: -1 The resulting JPEG picture height. default: -1 + * keepAspectRatio: Number + +Defines if cropping should keep the specified aspect ratio (1) or not (0). default: 1 + ## Ionic / Typescript Example Angular 2 Service diff --git a/package.json b/package.json index 48172b6..2b0152d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-crop", - "version": "0.4.0", + "version": "0.3.3", "description": "Crop an image in a Cordova app", "cordova": { "id": "cordova-plugin-crop", diff --git a/plugin.xml b/plugin.xml index c4de9a1..349c25b 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="0.3.3"> CropPlugin diff --git a/src/android/CropPlugin.java b/src/android/CropPlugin.java index e1dee93..fb3afaf 100644 --- a/src/android/CropPlugin.java +++ b/src/android/CropPlugin.java @@ -29,6 +29,7 @@ public boolean execute(String action, JSONArray args, final CallbackContext call JSONObject options = args.getJSONObject(1); int targetWidth = options.getInt("targetWidth"); int targetHeight = options.getInt("targetHeight"); + int keepAspectRatio = options.getInt("keepAspectRatio"); this.inputUri = Uri.parse(imagePath); this.outputUri = Uri.fromFile(new File(getTempDirectoryPath() + "/" + System.currentTimeMillis()+ "-cropped.jpg")); @@ -42,11 +43,18 @@ public boolean execute(String action, JSONArray args, final CallbackContext call Crop crop = Crop.of(this.inputUri, this.outputUri); if(targetHeight != -1 && targetWidth != -1) { crop.withMaxSize(targetWidth, targetHeight); - if(targetWidth == targetHeight) { - crop.asSquare(); + if(keepAspectRatio != 0) { + if(targetWidth == targetHeight) { + crop.asSquare(); + }else{ + crop.withAspect(targetWidth, targetHeight); + } } + } else { - crop.asSquare(); + if(keepAspectRatio != 0){ + crop.asSquare(); + } } crop.start(cordova.getActivity()); return true; diff --git a/src/ios/CTCrop.m b/src/ios/CTCrop.m index f3a50e6..f3e61a3 100644 --- a/src/ios/CTCrop.m +++ b/src/ios/CTCrop.m @@ -4,9 +4,10 @@ @interface CTCrop () @property (copy) NSString* callbackId; -@property (assign) NSUInteger quality; -@property (assign) NSUInteger targetWidth; -@property (assign) NSUInteger targetHeight; +@property (assign) NSInteger quality; +@property (assign) NSInteger targetWidth; +@property (assign) NSInteger targetHeight; +@property (assign) BOOL keepAspectRatio; @end @implementation CTCrop @@ -19,6 +20,7 @@ - (void) cropImage: (CDVInvokedUrlCommand *) command { self.quality = options[@"quality"] ? [options[@"quality"] intValue] : 100; self.targetWidth = options[@"targetWidth"] ? [options[@"targetWidth"] intValue] : -1; self.targetHeight = options[@"targetHeight"] ? [options[@"targetHeight"] intValue] : -1; + self.keepAspectRatio = options[@"keepAspectRatio"] ? [options[@"keepAspectRatio"] intValue] : 1; NSString *filePrefix = @"file://"; if ([imagePath hasPrefix:filePrefix]) { @@ -40,17 +42,27 @@ - (void) cropImage: (CDVInvokedUrlCommand *) command { cropController.delegate = self; cropController.image = image; - CGFloat width = self.targetWidth > -1 ? (CGFloat)self.targetWidth : image.size.width; - CGFloat height = self.targetHeight > -1 ? (CGFloat)self.targetHeight : image.size.height; - CGFloat length = MIN(width, height); + CGFloat width; + CGFloat height; + + if(self.targetWidth != -1 && self.targetHeight != -1){ + width = (CGFloat)self.targetWidth; + height = (CGFloat)self.targetHeight; + } else { + if(self.keepAspectRatio != 0){ + width = MIN(image.size.width, image.size.height); + height = width; + } else { + width = image.size.width; + height = image.size.height; + } + } + cropController.toolbarHidden = YES; cropController.rotationEnabled = NO; - cropController.keepingCropAspectRatio = YES; + cropController.keepingCropAspectRatio = self.keepAspectRatio == 0 ? NO : YES; - cropController.imageCropRect = CGRectMake((width - length) / 2, - (height - length) / 2, - length, - length); + cropController.imageCropRect = CGRectMake(0, 0, width, height); self.callbackId = command.callbackId; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:cropController]; diff --git a/www/crop.js b/www/crop.js index 31fd6e2..a6786ad 100644 --- a/www/crop.js +++ b/www/crop.js @@ -4,6 +4,7 @@ var crop = module.exports = function cropImage (success, fail, image, options) { options.quality = options.quality || 100 options.targetWidth = options.targetWidth || -1 options.targetHeight = options.targetHeight || -1 + options.keepAspectRatio = options.keepAspectRatio || options.keepAspectRatio===0 ? options.keepAspectRatio : 1 return cordova.exec(success, fail, 'CropPlugin', 'cropImage', [image, options]) }