Skip to content

Commit

Permalink
9.1.0 Scale the image to align with the crop rect and make crop rect …
Browse files Browse the repository at this point in the history
…as bigger as possible when rotate Image on Editor mode.(#713)
  • Loading branch information
zmtzawqlp committed Jan 3, 2025
1 parent 63684b1 commit 1c5b061
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
10 changes: 9 additions & 1 deletion lib/src/editor/crop_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,15 @@ class ExtendedImageCropLayerState extends State<ExtendedImageCropLayer>
);

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
Expand Down
47 changes: 47 additions & 0 deletions lib/src/editor/edit_action_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Offset> rectVertices = <Offset>[
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;
}
}
}

Expand All @@ -290,6 +315,28 @@ class EditActionDetails {
return scaleDelta;
}

double scaleToMatchRect(
List<Offset> 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<Offset> rectVertices,
Rect rect,
Expand Down
8 changes: 7 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit 1c5b061

Please sign in to comment.