Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

New option "keepAspectRatio" #77

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
## Install

```
$ cordova plugin add --save cordova-plugin-crop
$ cordova plugin add --save https://github.com/rastafan/cordova-plugin-crop
```


Expand Down Expand Up @@ -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

<img src="screenshot-example.png" width="250" height="500">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-crop"
version="0.3.1">
version="0.3.3">

<name>CropPlugin</name>

Expand Down
14 changes: 11 additions & 3 deletions src/android/CropPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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;
Expand Down
34 changes: 23 additions & 11 deletions src/ios/CTCrop.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]) {
Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions www/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down