Cropper: Minimum natural width/height options

Created on 19 Feb 2016  路  3Comments  路  Source: fengyuanchen/cropper

Firstly, this is a feature request rather than a bug. Sorry if this is the wrong place for it!

In my use-case I require the final image to be a minimum width and height. So far I've achieved this with the following:

var MIN_WIDTH  = 1200,
    MIN_HEIGHT = 800;

$('#cropper > img').cropper({
    aspectRatio: (MIN_WIDTH / MIN_HEIGHT),
    autoCropArea: 0.8,
    built: function () {
        var data = $(this).cropper('getData');

        // Ensure the default crop zone is never less than the minimum
        if (data.width < MIN_WIDTH || data.height < MIN_HEIGHT) {
            data.width  = MIN_WIDTH;
            data.height = MIN_HEIGHT;
        }

        $(this).cropper('setData', data);
    },
    dragmove: function () {
        // As above
    },
    dragend: function () {
        // As above
    }
});

Clearly this would be easier if there were options to achieve this out of the box, for example:

$('#cropper > img').cropper({
    aspectRatio: (MIN_WIDTH / MIN_HEIGHT),
    autoCropArea: 0.8,
    minImageWidth: MIN_WIDTH,
    minImageHeight: MIN_HEIGHT
});

Most helpful comment

@fengyuanchen

Please reopen this issue, in the older version (0.7.6) you could set the min cropper width and height. Those options still exist but instead it looks to the current page instead of calculating the size for the image that is being used.

I understand your comment that you should resize server side but sometimes you don't want people to crop an image below a size because of the quality of the cropped image.

All 3 comments

Please resize the cropped image to the final size on server side or:

var MIN_WIDTH  = 1200,
    MIN_HEIGHT = 800;

$().cropper({
  aspectRatio: (MIN_WIDTH / MIN_HEIGHT),
  built: function () {
    var croppedCanvas = $(this).cropper('getCroppedCanvas', {
      width: MIN_WIDTH,
      height: MIN_HEIGHT
    });

    console.log(croppedCanvas); // -> <canvas width="1200" height="800">

    var finalImage = croppedCanvas.toDataURL();
  }
});

I've updated to use v2.3.0, and combatted this issue with:

var MIN_WIDTH  = 1200,
    MIN_HEIGHT = 800;

$().on('cropmove.cropper', function () {
  var data = $(this).cropper('getData');

  if (data.width < MIN_WIDTH || data.height < MIN_HEIGHT) {
    data.width  = MIN_WIDTH;
    data.height = MIN_HEIGHT;

    $(this).cropper('setData', data);
  }
});

@fengyuanchen

Please reopen this issue, in the older version (0.7.6) you could set the min cropper width and height. Those options still exist but instead it looks to the current page instead of calculating the size for the image that is being used.

I understand your comment that you should resize server side but sometimes you don't want people to crop an image below a size because of the quality of the cropped image.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

steefaan picture steefaan  路  6Comments

richdenis86 picture richdenis86  路  8Comments

ThameDBA picture ThameDBA  路  4Comments

hpolonkoev picture hpolonkoev  路  5Comments

JSteunou picture JSteunou  路  7Comments