1

I would like to resize an image picked from the gallery. I've set targetHeight and targetWidth, but they seem to be ignored...

When picking an image from the camera, the setting allowEdit gives me the possibility to crop the image, but not when the pic is coming from gallery.

I've tried some Crop plugins, but they don't match my needs or i can't implement them on ionic V1.

Do you have an idea on what can i change in my code to have a 200px x 200px square pic when choosing it from the gallery ?

// source_type is either 'camera' or 'gallery'

$scope.takePicture = function(source_type)
{
    navigator.camera.getPicture(
    function(uri){

 $ionicPopup.alert({
          title: 'New avatar',
          template: 'Is uploading...'
 });
 
 // Create a root reference
   var storageRef = firebase.storage().ref();
    var newAvatar = "test.jpg";

    var contentType = contentType || '';
    var sliceSize = sliceSize || 512;

    let byteCharacters = atob(uri);
    let byteArrays = [];
           
    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) 
    {
        let slice = byteCharacters.slice(offset, offset + sliceSize);
           
        let byteNumbers = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) 
        {
            byteNumbers[i] = slice.charCodeAt(i);
        }
           
        let byteArray = new Uint8Array(byteNumbers);
           
        byteArrays.push(byteArray);
    }
           
    var mountainImagesRef = storageRef.child("storage/pics/"+newAvatar);
           
    var blob = new Blob(byteArrays);
           
    mountainImagesRef.put(blob).then(function(snapshot) 
    {
        console.log("Uploaded a base64 string!");
        $scope.addNewAvatar(newAvatar);
    });

    },
    function(){
    $ionicPopup.alert({
          title: 'Erreur',
          template: 'Error'
          });
    },
    {

      quality: 70,
     targetHeight: 200,
     allowEdit: true,
     targetWidth: 200,
      destinationType: 0,
    sourceType: source_type === 'gallery' ? 0 : 1,
      mediaType: 0,
      saveToPhotoAlbum: false
     }
     );
};
Memphis
  • 410
  • 5
  • 23
  • https://stackoverflow.com/questions/19262141/resize-image-with-javascript-canvas-smoothly/19262385 – Aaron Saunders Oct 21 '18 at 23:54
  • Actually I d'ont really understand where i should pick the image variable. Because i'm doing so much transformations on the `uri` (I thought options should be enough to select the image size, and it always was, except on Android when picking from the gallery...), i'm not sure to know where the img source is, and when i should apply on it, for exemple a canvas. I'd better be apply before storage on firebase. – Memphis Oct 22 '18 at 14:19

0 Answers0