3

Hello friends In my project, I'm able to capture the image via camera or gallery the size of the Image file vary to 5-6 MB based on the quality of camera or user selected image.Then I have to upload it on server.

The requirement is, before upload the file on remote server, I want to apply lossless compression on image to reduce the file size to 2-3MB so it will save internet cost of user & and my server space as well.

without using resize image. how can I do this?.

I have the user upload a Image. This is my code.

$scope.addImage = function (source) {
            $rootScope.ionPopup.close();
            var src = Camera.PictureSourceType.CAMERA
            if (source == 'PHOTOLIBRARY')
                src = Camera.PictureSourceType.PHOTOLIBRARY
            var options = {
                quality: 100,
                destinationType: Camera.DestinationType.DATA_URL,
                sourceType: src,
                allowEdit: false,
                encodingType: Camera.EncodingType.JPEG,
                targetWidth: 350,
                targetHeight: 350,
                //popoverOptions: CameraPopoverOptions,
                saveToPhotoAlbum: true,
                correctOrientation: true
            };
            $cordovaCamera.getPicture(options).then(function (imageData) {
                uploadImage(imageData);

            }, function (err) {
                // error
            });
        };
        var uploadImage = function (imageData) {
            $scope.orderImages.push(imageData);
            $scope.orderImagesList.push({ImageString: imageData});
        };

imageData is the base64 encoded image.

How can I compress that base64 encoded image ?

can someone help me !

Sanket Dorle
  • 117
  • 1
  • 12

1 Answers1

3

The targetHeight, and targetWidth options should change the resulting file size. I recently used:

quality: 90, 
targetWidth: 900,
targetHeight: 900,

to get a huge 10MB image file to scale down to about 200KB. Keep in mind that:

Photos selected from the device's gallery are not downscaled to a lower quality, even if a quality parameter is specified.

Source: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/index.html

Jeremy_inPDX
  • 1,049
  • 9
  • 9