Cropper: Min and max aspect ratio.

Created on 8 Feb 2015  路  7Comments  路  Source: fengyuanchen/cropper

Hello!
Thank you for your great plugin!

I'm using cropper.js for cover and avatar image cropping.
Users can upload cover image with fixed width(928px) and non-fixed height. But height of cropped image must be from 100px to 800px. So my question is: how can I assign to cropper min aspectratio as 928/100 and max aspectratio as 928/800.

Thank you in advance.

Most helpful comment

Here is a solution with cropmove event:

<div>
  <img id="image" src="/path/to/picture.jpg">
</div>
$(function () {
  var $image = $('#image');
  var minAspectRatio = 1;
  var maxAspectRatio = 2;

  $image.cropper({
    cropmove: function (e) {
      var cropBoxData = $image.cropper('getCropBoxData');
      var cropBoxWidth = cropBoxData.width;
      var aspectRatio = cropBoxWidth / cropBoxData.height;

      if (aspectRatio < minAspectRatio) {
        $image.cropper('setCropBoxData', {
          height: cropBoxWidth / minAspectRatio
        });
      } else if (aspectRatio > maxAspectRatio) {
        $image.cropper('setCropBoxData', {
          height: cropBoxWidth / maxAspectRatio
        });
      }
    }
  });
});

All 7 comments

I'm sorry, we don't support this right now.

+1, I would really love this to be possible as well (for http://www.twitshot.com)

+1

+1

I want to upload and crop images in two different size 60_40 and 130_100
Please suggest anyone how to do this ..using this plugin
Thanks for any suggestion....

if i want to fix size of crop box selector area on image before cropping any idea if....suggest
Thanks

Here is a solution with cropmove event:

<div>
  <img id="image" src="/path/to/picture.jpg">
</div>
$(function () {
  var $image = $('#image');
  var minAspectRatio = 1;
  var maxAspectRatio = 2;

  $image.cropper({
    cropmove: function (e) {
      var cropBoxData = $image.cropper('getCropBoxData');
      var cropBoxWidth = cropBoxData.width;
      var aspectRatio = cropBoxWidth / cropBoxData.height;

      if (aspectRatio < minAspectRatio) {
        $image.cropper('setCropBoxData', {
          height: cropBoxWidth / minAspectRatio
        });
      } else if (aspectRatio > maxAspectRatio) {
        $image.cropper('setCropBoxData', {
          height: cropBoxWidth / maxAspectRatio
        });
      }
    }
  });
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghengeveld picture ghengeveld  路  3Comments

zilions picture zilions  路  3Comments

ThameDBA picture ThameDBA  路  4Comments

hpolonkoev picture hpolonkoev  路  5Comments

nchase picture nchase  路  5Comments