cordova-plugin-crop
Crop an image in a Cordova app
Install
$ cordova plugin add --save cordova-plugin-crop
Usage
plugins.crop(function success () {
}, function fail () {
}, '/path/to/image', options)
or, if you are running on an environment that supports Promises
(Crosswalk, Android >= KitKat, iOS >= 8)
plugins.crop.promise('/path/to/image', options)
.then(function success (newPath) {
})
.catch(function fail (err) {
})
API
The resulting JPEG quality (ignored on Android). default: 100
The resulting JPEG picture width. default: -1
The resulting JPEG picture height. default: -1
Ionic / Typescript Example Angular 2 Service
This is an example service that uses ionic-native's built in camera and the cordova-plugin-crop to create a cropped version of the image and return the file path.
import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Camera, Crop } from 'ionic-native';
@Injectable()
export class CameraService {
public options: any = {
allowEdit: true,
sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
mediaType: Camera.MediaType.ALLMEDIA,
destinationType: Camera.DestinationType.FILE_URI
}
constructor(public platform: Platform) {}
getMedia(): Promise<any> {
return Camera.getPicture(this.options)
.then((fileUri) => {
if (this.platform.is('ios')) {
return fileUri
} else if (this.platform.is('android')) {
fileUri = 'file://' + fileUri;
return Crop.crop(fileUri, { quality: 100, targetWidth: -1, targetHeight: -1 });
}
})
.then((path) => {
console.log('Cropped Image Path!: ' + path);
return path;
})
}
}
Libraries used
License
MIT © Jeduan Cornejo