fengyuanchen / cropper

⚠️ [Deprecated] No longer maintained, please use https://github.com/fengyuanchen/jquery-cropper
MIT License
7.75k stars 1.74k forks source link

Fix Bug for angles > 90º #950

Closed david-lorenzo88 closed 6 years ago

david-lorenzo88 commented 6 years ago

Hello, There's a small bug that apply only for rotation angles greater than 90 degrees or less than -90º, and generates and incorrect crop coordinates that when sent to the server side, derivates in incorrect cropped images.

The bug is located in getRotatedSizes function of cropper.js:

  1. Firstly we need to module the degrees by 180 to limit the angle calculation, as the plugin gives you a 360º based angle.
    degree = _ref5.degree % 180;
  2. Lastly, when converting degrees to radians, we need to get the inverted angle when the original angle is greater than 90º.

    var arc = (degree > 90 ? (180 - degree) : degree) * Math.PI / 180;

    This is the modified function:

    /**
    * Get the new sizes of a rectangle after rotated.
    * @param {Object} data - The original sizes.
    * @returns {Object} The result sizes.
    */
    function getRotatedSizes(_ref5) {
    var width = _ref5.width,
      height = _ref5.height,
      degree = _ref5.degree % 180;
    
    degree = Math.abs(degree);
    
    if (degree % 180 === 90) {
    return {
      width: height,
      height: width
    };
    }
    
    var arc = (degree > 90 ? (180 - degree) : degree) * Math.PI / 180;
    var sinArc = Math.sin(arc);
    var cosArc = Math.cos(arc);
    
    return {
    width: width * cosArc + height * sinArc,
    height: width * sinArc + height * cosArc
    };
    }
fengyuanchen commented 6 years ago

What are you doing?

david-lorenzo88 commented 6 years ago

Hello,

I am using your plugin to crop an image in a modal. I am using your crop example with the php side script. But coordinates sent by cropped.js when rotation angle is greater than 90° are incorrect . As O explained you in the pull request.

Best David

El 9 ene. 2018 12:21, "Chen Fengyuan" notifications@github.com escribió:

What are you doing?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fengyuanchen/cropper/pull/950#issuecomment-356268997, or mute the thread https://github.com/notifications/unsubscribe-auth/AA94V99ni_GywxXDMZrKeeteKfjF5PE5ks5tI1nDgaJpZM4RWziu .

fengyuanchen commented 6 years ago

I mean why pull the commits that I had already committed before again?

And I had already fixed this problem about a month ago!

david-lorenzo88 commented 6 years ago

Ok maybe I did the wrong thing to submit the change. In the version I downloaded a week ago is not corrected. The version number of cropper.js is 3.1.3 from 2017-10-21

El 9 ene. 2018 12:57, "Chen Fengyuan" notifications@github.com escribió:

I mean why pull the commits that I had already committed before?

And I had already fixed this problem about a month ago https://github.com/fengyuanchen/cropper/pull/950/commits/91a76392992a6bec19bd9630360a173ee1ecce1f !

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fengyuanchen/cropper/pull/950#issuecomment-356276646, or mute the thread https://github.com/notifications/unsubscribe-auth/AA94Vxu5PuL7euxjpkBczoT-J3rcpoNMks5tI2I6gaJpZM4RWziu .

fengyuanchen commented 6 years ago

The problem is fixed but not release yet.

david-lorenzo88 commented 6 years ago

Ok sorry for the confusion.

El 9 ene. 2018 13:10, "Chen Fengyuan" notifications@github.com escribió:

The problem is fixed but not release yet.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fengyuanchen/cropper/pull/950#issuecomment-356279505, or mute the thread https://github.com/notifications/unsubscribe-auth/AA94V4Jxd9aoklC5_DOLAh7MNSQhFKWgks5tI2VEgaJpZM4RWziu .