Fengyuanchen cropper zooms too much and loss of quality for images

1.2k views Asked by At

I started from Crop Avatar exemple for my app with this configurations

this.$img.cropper({
          preview: this.$avatarPreview.selector,
          viewMode: 2,
          dragMode: 'move',
          guides: false,
          highlight: false,
          autoCropArea: 1,
          movable: true,
          strict: true,
          cropBoxResizable: false,
          minCropBoxWidth: 1000,
          minCropBoxHeight: 1000,
          zoom: function (e) {
            if (e.ratio > 1) {
              e.preventDefault();
              $(this).cropper('zoomTo', 1);
            }
          },

Using it like this I can zoom to ratio 1 but for smaller images is too much. For example a 1200x1200 image can be zoomed with ratio 1 to a level that saving it to 1000x1000 it loses to much quality. I tried to go under 1 with $(this).cropper('zoomTo', 1); but it makes a strange effect.If i zoom to much it brings me to the original size.

My question is how can i block the zoom to something like x0.2 or a reasonable value.Thank you

1

There are 1 answers

0
Claudiu Ep On

This answer comes from fengyuanchen who developed the cropper and I think fits my need for different resolutions of the cropbox on different devices

$().cropper({
  zoom: function (e) {
    var container = $(this).cropper('getContainerData');

    // Desktop
    if (container.width > 1120) {

      // Zoom in
      if (e.ratio > e.oldRatio) {
        if (e.ratio > 10) {
          e.preventDefault();
        }

      // Zoom out
      } else {
        if (e.ratio < 0.1) {
          e.preventDefault();
        }
      }

    // Laptop
    } else if (container.width > 992) {
      // ...

    // Tablet
    } else if (container.width > 768) {
      // ...

    // Phone
    } else {
      // ...
    }
  }
});