|
4602 | 4602 | * @return {Object} Options
|
4603 | 4603 | */
|
4604 | 4604 | calculateImageSelectOptions: function( attachment, controller ) {
|
4605 |
| - var control = controller.get( 'control' ), |
4606 |
| - flexWidth = !! parseInt( control.params.flex_width, 10 ), |
4607 |
| - flexHeight = !! parseInt( control.params.flex_height, 10 ), |
4608 |
| - realWidth = attachment.get( 'width' ), |
4609 |
| - realHeight = attachment.get( 'height' ), |
4610 |
| - xInit = parseInt( control.params.width, 10 ), |
4611 |
| - yInit = parseInt( control.params.height, 10 ), |
4612 |
| - ratio = xInit / yInit, |
4613 |
| - xImg = xInit, |
4614 |
| - yImg = yInit, |
| 4605 | + var control = controller.get( 'control' ), |
| 4606 | + flexWidth = !! parseInt( control.params.flex_width, 10 ), |
| 4607 | + flexHeight = !! parseInt( control.params.flex_height, 10 ), |
| 4608 | + realWidth = attachment.get( 'width' ), |
| 4609 | + realHeight = attachment.get( 'height' ), |
| 4610 | + xInit = parseInt( control.params.width, 10 ), |
| 4611 | + yInit = parseInt( control.params.height, 10 ), |
| 4612 | + requiredRatio = xInit / yInit, |
| 4613 | + realRatio = realWidth / realHeight, |
| 4614 | + xImg = xInit, |
| 4615 | + yImg = yInit, |
4615 | 4616 | x1, y1, imgSelectOptions;
|
4616 | 4617 |
|
| 4618 | + controller.set( 'hasRequiredAspectRatio', control.hasRequiredAspectRatio( requiredRatio, realRatio ) ); |
| 4619 | + controller.set( 'suggestedCropSize', { width: realWidth, height: realHeight, x1: 0, y1: 0, x2: xInit, y2: yInit } ); |
4617 | 4620 | controller.set( 'canSkipCrop', ! control.mustBeCropped( flexWidth, flexHeight, xInit, yInit, realWidth, realHeight ) );
|
4618 | 4621 |
|
4619 |
| - if ( realWidth / realHeight > ratio ) { |
| 4622 | + if ( realRatio > requiredRatio ) { |
4620 | 4623 | yInit = realHeight;
|
4621 |
| - xInit = yInit * ratio; |
| 4624 | + xInit = yInit * requiredRatio; |
4622 | 4625 | } else {
|
4623 | 4626 | xInit = realWidth;
|
4624 |
| - yInit = xInit / ratio; |
| 4627 | + yInit = xInit / requiredRatio; |
4625 | 4628 | }
|
4626 | 4629 |
|
4627 | 4630 | x1 = ( realWidth - xInit ) / 2;
|
|
4662 | 4665 | /**
|
4663 | 4666 | * Return whether the image must be cropped, based on required dimensions.
|
4664 | 4667 | *
|
4665 |
| - * @param {boolean} flexW |
4666 |
| - * @param {boolean} flexH |
4667 |
| - * @param {number} dstW |
4668 |
| - * @param {number} dstH |
4669 |
| - * @param {number} imgW |
4670 |
| - * @param {number} imgH |
4671 |
| - * @return {boolean} |
| 4668 | + * @param {boolean} flexW Width is flexible. |
| 4669 | + * @param {boolean} flexH Height is flexible. |
| 4670 | + * @param {number} dstW Required width. |
| 4671 | + * @param {number} dstH Required height. |
| 4672 | + * @param {number} imgW Provided image's width. |
| 4673 | + * @param {number} imgH Provided image's height. |
| 4674 | + * @return {boolean} Whether cropping is required. |
4672 | 4675 | */
|
4673 | 4676 | mustBeCropped: function( flexW, flexH, dstW, dstH, imgW, imgH ) {
|
4674 | 4677 | if ( true === flexW && true === flexH ) {
|
|
4694 | 4697 | return true;
|
4695 | 4698 | },
|
4696 | 4699 |
|
| 4700 | + /** |
| 4701 | + * Check if the image's aspect ratio essentially matches the required aspect ratio. |
| 4702 | + * |
| 4703 | + * Floating point precision is low, so this allows a small tolerance. This |
| 4704 | + * tolerance allows for images over 100,000 px on either side to still trigger |
| 4705 | + * the cropping flow. |
| 4706 | + * |
| 4707 | + * @param {number} requiredRatio Required image ratio. |
| 4708 | + * @param {number} realRatio Provided image ratio. |
| 4709 | + * @return {boolean} Whether the image has the required aspect ratio. |
| 4710 | + */ |
| 4711 | + hasRequiredAspectRatio: function ( requiredRatio, realRatio ) { |
| 4712 | + if ( Math.abs( requiredRatio - realRatio ) < 0.000001 ) { |
| 4713 | + return true; |
| 4714 | + } |
| 4715 | + |
| 4716 | + return false; |
| 4717 | + }, |
| 4718 | + |
4697 | 4719 | /**
|
4698 | 4720 | * If cropping was skipped, apply the image data directly to the setting.
|
4699 | 4721 | */
|
|
0 commit comments