Search code examples
ionic-frameworkcordova-pluginscrop

ionic 1.x : crop image


I have two buttons, the first one for browse image from gallery and the second one for taking photo. I'm using cordova camera plugin for two cases.

After choosing an image, I want to crop it before to send to server using cordova file transfer plugin. I've tried to use several plugins such as jr-crop, angular-image-crop, ngImgCrop. The problem is that plugins returns a base64 image, but I want to get the image url (not dataUrl). Any help please !


Solution

  • My solution (@egycode) :

    $scope.image_gallery = function() {
        var options = {
            quality: 100,
            correctOrientation: true,
            sourceType: 0
        };
        $cordovaCamera.getPicture(options).then(function(data) {
            console.log(data);
            $scope.crop(data);
            console.log('camera data image_gallery: ' + angular.toJson(data));
        }, function(error) {
            console.log('camera error image_gallery: ' + angular.toJson(error));
        });
    }
    
    $scope.crop = function(url) {
        $jrCrop.crop({
            url: url,
            width: 261,
            height: 362
        }).then(function(canvas) {
            console.log(canvas);
            var image = canvas.toDataURL();
            //var image is the result, you can show it using : $scope.pictureUrl = image;
        }, function() {
            // User canceled or couldn't load image.
        });
    }