From 1c5b06134f4f3adee5a7b61f3ee9ad1d4255f1d9 Mon Sep 17 00:00:00 2001 From: zmtzawqlp Date: Fri, 3 Jan 2025 21:33:11 +0800 Subject: [PATCH] 9.1.0 Scale the image to align with the crop rect and make crop rect as bigger as possible when rotate Image on Editor mode.(#713) --- CHANGELOG.md | 3 +- lib/src/editor/crop_layer.dart | 10 +++++- lib/src/editor/edit_action_details.dart | 47 +++++++++++++++++++++++++ pubspec.yaml | 8 ++++- 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 834fa99..4a27586 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -## Unreleased +## 9.1.0 * Removed the `enableMemoryCache` variable in `ExtendedImage`,please Use `clearMemoryCacheWhenDispose` instead for managing memory cache behavior. +* Scale the image to align with the crop rect and make crop rect as bigger as possible when rotate Image on Editor mode.(#713) ## 9.0.9 diff --git a/lib/src/editor/crop_layer.dart b/lib/src/editor/crop_layer.dart index 091e0cd..ff53b4d 100644 --- a/lib/src/editor/crop_layer.dart +++ b/lib/src/editor/crop_layer.dart @@ -640,7 +640,15 @@ class ExtendedImageCropLayerState extends State ); if (scaleDelta <= 0) { - return; + // make crop rect as bigger as possible + final double scaleDelta1 = + widget.editActionDetails.scaleToMatchRect(rectVertices, layoutRect); + if (scaleDelta1 != double.negativeInfinity && + !scaleDelta1.equalTo(1.0)) { + scaleDelta = scaleDelta1; + } else { + return; + } } // not out of layout rect diff --git a/lib/src/editor/edit_action_details.dart b/lib/src/editor/edit_action_details.dart index df4579f..d719b51 100644 --- a/lib/src/editor/edit_action_details.dart +++ b/lib/src/editor/edit_action_details.dart @@ -265,6 +265,31 @@ class EditActionDetails { preTotalScale = totalScale; totalScale = totalScale * scaleDelta; } + } else { + // scale the image to align with the crop rect + final Matrix4 result = getTransform(); + result.invert(); + final Rect rect = _screenDestinationRect!; + final List rectVertices = [ + screenCropRect!.topLeft, + screenCropRect!.topRight, + screenCropRect!.bottomRight, + screenCropRect!.bottomLeft, + ].map((Offset element) { + final Vector4 cornerVector = Vector4(element.dx, element.dy, 0.0, 1.0); + final Vector4 newCornerVector = result.transform(cornerVector); + return Offset(newCornerVector.x, newCornerVector.y); + }).toList(); + + final double scaleDelta = scaleToMatchRect( + rectVertices, + rect, + ); + if (scaleDelta != double.negativeInfinity) { + screenFocalPoint = null; + preTotalScale = totalScale; + totalScale = totalScale * scaleDelta; + } } } @@ -290,6 +315,28 @@ class EditActionDetails { return scaleDelta; } + double scaleToMatchRect( + List rectVertices, + Rect rect, + ) { + double scaleDelta = double.negativeInfinity; + final Offset center = rect.center; + for (final Offset element in rectVertices) { + if (!rect.containsOffset(element)) { + continue; + } + final double x = (element.dx - center.dx).abs(); + final double y = (element.dy - center.dy).abs(); + final double halfWidth = rect.width / 2; + final double halfHeight = rect.height / 2; + if (x < halfWidth || y < halfHeight) { + scaleDelta = max(scaleDelta, max(x / halfWidth, y / halfHeight)); + } + } + + return scaleDelta; + } + double scaleToFit( List rectVertices, Rect rect, diff --git a/pubspec.yaml b/pubspec.yaml index 4a9f864..79615f7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,14 @@ name: extended_image description: Official extension image, support placeholder(loading)/ failed state, cache network, zoom/pan, photo view, slide out page, editor(crop,rotate,flip), painting etc. -version: 9.0.9 +version: 9.1.0 repository: https://github.com/fluttercandies/extended_image issue_tracker: https://github.com/fluttercandies/extended_image/issues +topics: + - image + - cropper + - photo-view + - editor + - slide-out environment: sdk: '>=2.18.0 <4.0.0'