diff --git a/src/js/_enqueues/lib/image-edit.js b/src/js/_enqueues/lib/image-edit.js
index 099398b4e3..270c7eb46d 100644
a
|
b
|
|
107 | 107 | * @return {void} |
108 | 108 | */ |
109 | 109 | init : function(postid) { |
110 | | var t = this, old = $('#image-editor-' + t.postid), |
111 | | x = t.intval( $('#imgedit-x-' + postid).val() ), |
112 | | y = t.intval( $('#imgedit-y-' + postid).val() ); |
| 110 | var t = this, old = $('#image-editor-' + t.postid); |
113 | 111 | |
114 | 112 | if ( t.postid !== postid && old.length ) { |
115 | 113 | t.close(t.postid); |
116 | 114 | } |
117 | 115 | |
118 | | t.hold.w = t.hold.ow = x; |
119 | | t.hold.h = t.hold.oh = y; |
120 | | t.hold.xy_ratio = x / y; |
121 | 116 | t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() ); |
122 | 117 | t.postid = postid; |
123 | 118 | $('#imgedit-response-' + postid).empty(); |
… |
… |
|
141 | 136 | $( document ).on( 'image-editor-ui-ready', this.focusManager ); |
142 | 137 | }, |
143 | 138 | |
| 139 | /** |
| 140 | * Calculate the image size and save it to memory. |
| 141 | * |
| 142 | * @since 5.8.0 - Fix cropping issues. |
| 143 | * |
| 144 | * @memberof imageEdit |
| 145 | * |
| 146 | * @param {number} postid The post ID. |
| 147 | * |
| 148 | * @return {void} |
| 149 | */ |
| 150 | calculateImgSize: function (postid) { |
| 151 | var t = this, |
| 152 | x = t.intval( $('#imgedit-x-' + postid).val() ), |
| 153 | y = t.intval( $('#imgedit-y-' + postid).val() ); |
| 154 | t.hold.w = t.hold.ow = x; |
| 155 | t.hold.h = t.hold.oh = y; |
| 156 | t.hold.xy_ratio = x / y; |
| 157 | t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() ); |
| 158 | t.currentCropSelection = null; |
| 159 | }, |
| 160 | |
144 | 161 | /** |
145 | 162 | * Toggles the wait/load icon in the editor. |
146 | 163 | * |
… |
… |
|
328 | 345 | for ( n in history ) { |
329 | 346 | i = history[n]; |
330 | 347 | if ( i.hasOwnProperty('c') ) { |
331 | | op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h } }; |
| 348 | op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h, 'r': i.c.r } }; |
332 | 349 | } else if ( i.hasOwnProperty('r') ) { |
333 | 350 | op[n] = { 'r': i.r.r }; |
334 | 351 | } else if ( i.hasOwnProperty('f') ) { |
… |
… |
|
661 | 678 | if ( 'undefined' === typeof this.hold.sizer ) { |
662 | 679 | this.init( postid ); |
663 | 680 | } |
| 681 | this.calculateImgSize( postid ); |
664 | 682 | |
665 | 683 | this.initCrop(postid, img, parent); |
666 | 684 | this.setCropSelection( postid, { 'x1': 0, 'y1': 0, 'x2': 0, 'y2': 0, 'width': img.innerWidth(), 'height': img.innerHeight() } ); |
… |
… |
|
744 | 762 | * |
745 | 763 | * @return {void} |
746 | 764 | */ |
747 | | parent.children().on( 'mousedown, touchstart', function(e){ |
748 | | var ratio = false, sel, defRatio; |
749 | | |
750 | | if ( e.shiftKey ) { |
751 | | sel = t.iasapi.getSelection(); |
752 | | defRatio = t.getSelRatio(postid); |
753 | | ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio; |
| 765 | parent.children().on( 'mousedown touchstart', function(e) { |
| 766 | var ratio = false, |
| 767 | sel = t.iasapi.getSelection(), |
| 768 | cx = t.intval($('#imgedit-crop-width-' + postid).val()), |
| 769 | cy = t.intval($('#imgedit-crop-height-' + postid).val()); |
| 770 | |
| 771 | if ( cx && cy ) { |
| 772 | ratio = t.getSelRatio(postid); |
| 773 | } else if ( e.shiftKey && sel && sel.width && sel.height ) { |
| 774 | ratio = sel.width + ':' + sel.height; |
754 | 775 | } |
755 | 776 | |
756 | 777 | t.iasapi.setOptions({ |
… |
… |
|
795 | 816 | */ |
796 | 817 | onSelectChange: function(img, c) { |
797 | 818 | var sizer = imageEdit.hold.sizer; |
798 | | selW.val( imageEdit.round(c.width / sizer) ); |
799 | | selH.val( imageEdit.round(c.height / sizer) ); |
| 819 | |
| 820 | var oldSel = imageEdit.currentCropSelection; |
| 821 | if (oldSel != null && oldSel.width == c.width && oldSel.height == c.height) { |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | selW.val( Math.min(imageEdit.hold.w, imageEdit.round(c.width / sizer)) ); |
| 826 | selH.val( Math.min(imageEdit.hold.h, imageEdit.round(c.height / sizer)) ); |
| 827 | |
| 828 | t.currentCropSelection = c; |
800 | 829 | } |
801 | 830 | }); |
802 | 831 | }, |
… |
… |
|
814 | 843 | * @return {boolean} |
815 | 844 | */ |
816 | 845 | setCropSelection : function(postid, c) { |
817 | | var sel; |
| 846 | var sel, |
| 847 | selW = $('#imgedit-sel-width-' + postid), |
| 848 | selH = $('#imgedit-sel-height-' + postid), |
| 849 | sizer = this.hold.sizer, |
| 850 | hold = this.hold; |
818 | 851 | |
819 | 852 | c = c || 0; |
820 | 853 | |
… |
… |
|
827 | 860 | return false; |
828 | 861 | } |
829 | 862 | |
830 | | sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height }; |
| 863 | // adjust the selection within the bounds of the image on 100% scale |
| 864 | var excessW = hold.w - ( Math.round(c.x1 / sizer) + parseInt(selW.val()) ); |
| 865 | var excessH = hold.h - ( Math.round(c.y1 / sizer) + parseInt(selH.val()) ); |
| 866 | var x = Math.round( c.x1 / sizer ) + Math.min( 0, excessW ); |
| 867 | var y = Math.round( c.y1 / sizer ) + Math.min( 0, excessH ); |
| 868 | |
| 869 | // use 100% scaling to prevent rounding errors |
| 870 | sel = { 'r': 1, 'x': x, 'y': y, 'w': selW.val(), 'h': selH.val() }; |
| 871 | |
831 | 872 | this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1); |
832 | 873 | $('#imgedit-selection-' + postid).val( JSON.stringify(sel) ); |
833 | 874 | }, |
… |
… |
|
955 | 996 | } |
956 | 997 | |
957 | 998 | this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce); |
| 999 | |
| 1000 | // Clear the selection fields after rotating. |
| 1001 | $('#imgedit-sel-width-' + postid).val(''); |
| 1002 | $('#imgedit-sel-height-' + postid).val(''); |
| 1003 | this.currentCropSelection = null; |
958 | 1004 | }, |
959 | 1005 | |
960 | 1006 | /** |
… |
… |
|
977 | 1023 | } |
978 | 1024 | |
979 | 1025 | this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce); |
| 1026 | |
| 1027 | // Clear the selection fields after flipping. |
| 1028 | $('#imgedit-sel-width-' + postid).val(''); |
| 1029 | $('#imgedit-sel-height-' + postid).val(''); |
| 1030 | this.currentCropSelection = null; |
980 | 1031 | }, |
981 | 1032 | |
982 | 1033 | /** |
… |
… |
|
1011 | 1062 | // Clear the selection fields after cropping. |
1012 | 1063 | $('#imgedit-sel-width-' + postid).val(''); |
1013 | 1064 | $('#imgedit-sel-height-' + postid).val(''); |
| 1065 | this.currentCropSelection = null; |
1014 | 1066 | }, |
1015 | 1067 | |
1016 | 1068 | /** |
… |
… |
|
1098 | 1150 | img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(), |
1099 | 1151 | sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi; |
1100 | 1152 | |
| 1153 | this.currentCropSelection = null; |
| 1154 | |
1101 | 1155 | if ( false === this.validateNumeric( el ) ) { |
1102 | 1156 | return; |
1103 | 1157 | } |
… |
… |
|
1121 | 1175 | if ( x2 > imgw ) { |
1122 | 1176 | x1 = 0; |
1123 | 1177 | x2 = imgw; |
1124 | | elX.val( Math.round( x2 / sizer ) ); |
| 1178 | elX.val( Math.min( this.hold.w, Math.round( x2 / sizer ) ) ); |
1125 | 1179 | } |
1126 | 1180 | |
1127 | 1181 | if ( y2 > imgh ) { |
1128 | 1182 | y1 = 0; |
1129 | 1183 | y2 = imgh; |
1130 | | elY.val( Math.round( y2 / sizer ) ); |
| 1184 | elY.val( Math.min( this.hold.h, Math.round( y2 / sizer ) ) ); |
1131 | 1185 | } |
1132 | 1186 | |
1133 | 1187 | ias.setSelection( x1, y1, x2, y2 ); |
1134 | 1188 | ias.update(); |
1135 | 1189 | this.setCropSelection(postid, ias.getSelection()); |
| 1190 | this.currentCropSelection = ias.getSelection(); |
1136 | 1191 | } |
1137 | 1192 | }, |
1138 | 1193 | |
diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php
index a91f082bb6..0ebe794315 100644
a
|
b
|
function image_edit_apply_changes( $image, $changes ) { |
628 | 628 | $w = $size['width']; |
629 | 629 | $h = $size['height']; |
630 | 630 | |
631 | | $scale = 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling. |
| 631 | $scale = isset($sel->r) ? $sel->r : 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling. |
632 | 632 | $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); |
633 | 633 | } else { |
634 | | $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling. |
| 634 | $scale = isset($sel->r) ? $sel->r : 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling. |
635 | 635 | $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); |
636 | 636 | } |
637 | 637 | break; |