code: function (imagen, width, height, quality, type, ratio, centerCrop, resizeIf) {
      var deferred = $q.defer();
      var canvasElement = document.createElement('canvas');
      var imageElement = document.createElement('img');
      imageElement.setAttribute('style', 'visibility:hidden;position:fixed;z-index:-100000');
      document.body.appendChild(imageElement);
  
      imageElement.onload = function () {
        var imgWidth = imageElement.width, imgHeight = imageElement.height;
        imageElement.parentNode.removeChild(imageElement);
        if (resizeIf != null && resizeIf(imgWidth, imgHeight) === false) {
          deferred.reject('resizeIf');
          return;
        }
        try {
          if (ratio) {
            var ratioFloat = upload.ratioToFloat(ratio);
            var imgRatio = imgWidth / imgHeight;
            if (imgRatio < ratioFloat) {
              width = imgWidth;
              height = width / ratioFloat;
            } else {
              height = imgHeight;
              width = height * ratioFloat;
            }
          }
          if (!width) {
            width = imgWidth;
          }
          if (!height) {
            height = imgHeight;
          }
          var dimensions = calculateAspectRatioFit(imgWidth, imgHeight, width, height, centerCrop);
          canvasElement.width = Math.min(dimensions.width, width);
          canvasElement.height = Math.min(dimensions.height, height);
          var context = canvasElement.getContext('2d');
          context.drawImage(imageElement,
            Math.min(0, -dimensions.marginX / 2), Math.min(0, -dimensions.marginY / 2),
            dimensions.width, dimensions.height);
          deferred.resolve(canvasElement.toDataURL(type || 'image/WebP', quality || 0.934));
        } catch (e) {
          deferred.reject(e);
        }
      };
      imageElement.onerror = function () {
        imageElement.parentNode.removeChild(imageElement);
        deferred.reject();
      };
      imageElement.src = imagen;
      return deferred.promise;
    }
docs: Extracted from https://github.com/romelgomez/angular-firebase-image-upload/blob/master/app/scripts/fileUpload.js#L89