Skip to content

Commit 45d01a9

Browse files
Version bump + fix issue with imagefit and forceInsideCropArea
1 parent 5692bf0 commit 45d01a9

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.1.1] - 2024-12-30
2+
3+
- Fixed issues with imagefit and forceInsideCropArea resulting in crops outside the crop area and/or wrong crops
4+
15
## [0.1.0] - 2024-12-30
26

37
- Added maskShape so you can crop using a different mask than for visualisation (e.g. circle mask for visualisation, but square mask for cropping)

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class _MyHomePageState extends State<MyHomePage> {
102102
Expanded(
103103
child: CustomImageCrop(
104104
cropController: controller,
105+
// forceInsideCropArea: true,
105106
image: const AssetImage(
106107
'assets/test.png'), // Any Imageprovider will work, try with a AssetImage or NetworkImage for example...
107108
// image: const NetworkImage('https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'),

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ packages:
4949
path: ".."
5050
relative: true
5151
source: path
52-
version: "0.1.0"
52+
version: "0.1.1"
5353
fake_async:
5454
dependency: transitive
5555
description:

lib/src/calculators/calculate_crop_fit_params.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CropFitParams calculateCropFitParams({
1212
required int imageHeight,
1313
required CustomImageFit imageFit,
1414
required double aspectRatio,
15+
required bool forceInsideCropArea,
1516
}) {
1617
/// the width of the area to crop
1718
final double cropSizeWidth;
@@ -20,7 +21,7 @@ CropFitParams calculateCropFitParams({
2021
final double cropSizeHeight;
2122

2223
/// used to adjust image scale
23-
final double defaultScale;
24+
double defaultScale;
2425

2526
switch (imageFit) {
2627
case CustomImageFit.fillCropSpace:
@@ -118,6 +119,15 @@ CropFitParams calculateCropFitParams({
118119
break;
119120
}
120121

122+
if (forceInsideCropArea) {
123+
if (imageWidth * defaultScale < cropSizeWidth) {
124+
defaultScale = cropSizeWidth / imageWidth;
125+
}
126+
if (imageHeight * defaultScale < cropSizeHeight) {
127+
defaultScale = cropSizeHeight / imageHeight;
128+
}
129+
}
130+
121131
return CropFitParams(
122132
cropSizeWidth: cropSizeWidth,
123133
cropSizeHeight: cropSizeHeight,

lib/src/calculators/calculate_on_crop_params.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:custom_image_crop/src/models/params_model.dart';
44
import 'package:custom_image_crop/src/widgets/custom_image_crop_widget.dart';
55

66
/// Returns params to use for cropping image.
7-
OnCropParams caclulateOnCropParams({
7+
OnCropParams calculateOnCropParams({
88
required double screenWidth,
99
required double screenHeight,
1010
required double cropPercentage,
@@ -13,6 +13,7 @@ OnCropParams caclulateOnCropParams({
1313
required int imageWidth,
1414
required int imageHeight,
1515
required CustomImageFit imageFit,
16+
required bool forceInsideCropArea,
1617
}) {
1718
/// the size of the area to crop (width and/or height depending on the aspect ratio)
1819
final double cropSizeMax;
@@ -21,7 +22,7 @@ OnCropParams caclulateOnCropParams({
2122
final double translateScale;
2223

2324
/// used to adjust image scale
24-
final double scale;
25+
double scale;
2526

2627
/// Temp variable used to calculate the translateScale
2728
final double uiSize;
@@ -55,16 +56,16 @@ OnCropParams caclulateOnCropParams({
5556
break;
5657

5758
case CustomImageFit.fillCropWidth:
58-
uiSize = screenWidth;
5959
cropSizeMax = imageWidth / min(1, aspectRatio);
60-
translateScale = cropSizeMax / (uiSize * cropPercentage);
60+
translateScale =
61+
cropSizeMax / (min(screenWidth, screenHeight) * cropPercentage);
6162
scale = dataScale;
6263
break;
6364

6465
case CustomImageFit.fillCropHeight:
65-
uiSize = screenHeight;
6666
cropSizeMax = imageHeight * max(1, aspectRatio);
67-
translateScale = cropSizeMax / (uiSize * cropPercentage);
67+
translateScale =
68+
cropSizeMax / (min(screenWidth, screenHeight) * cropPercentage);
6869
scale = dataScale;
6970
break;
7071

@@ -137,6 +138,19 @@ OnCropParams caclulateOnCropParams({
137138
cropSizeHeight = cropSizeMax / aspectRatio;
138139
cropSizeWidth = cropSizeHeight * aspectRatio;
139140
}
141+
142+
if (forceInsideCropArea) {
143+
final defaultScale = scale / dataScale;
144+
var newDefaultScale = defaultScale;
145+
if (imageWidth * defaultScale < cropSizeWidth) {
146+
newDefaultScale = cropSizeWidth / imageWidth;
147+
}
148+
if (imageHeight * defaultScale < cropSizeHeight) {
149+
newDefaultScale = cropSizeHeight / imageHeight;
150+
}
151+
scale = scale / defaultScale * newDefaultScale;
152+
}
153+
140154
return OnCropParams(
141155
cropSizeHeight: cropSizeHeight,
142156
cropSizeWidth: cropSizeWidth,

lib/src/widgets/custom_image_crop_widget.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ class _CustomImageCropState extends State<CustomImageCrop>
258258
screenHeight: _height,
259259
screenWidth: _width,
260260
aspectRatio: (widget.ratio?.width ?? 1) / (widget.ratio?.height ?? 1),
261+
forceInsideCropArea: widget.forceInsideCropArea,
261262
);
262263
final scale = data.scale * cropFitParams.additionalScale;
263264
_path = _getPath(
@@ -384,10 +385,10 @@ class _CustomImageCropState extends State<CustomImageCrop>
384385
screenHeight: _height,
385386
screenWidth: _width,
386387
aspectRatio: (widget.ratio?.width ?? 1) / (widget.ratio?.height ?? 1),
388+
forceInsideCropArea: widget.forceInsideCropArea,
387389
);
388-
final initialWidth = _imageAsUIImage!.width * cropFitParams.additionalScale;
389-
final initialHeight =
390-
_imageAsUIImage!.height * cropFitParams.additionalScale;
390+
final initialWidth = image.width * cropFitParams.additionalScale;
391+
final initialHeight = image.height * cropFitParams.additionalScale;
391392
return Rect.fromLTWH(
392393
(_width - initialWidth) / 2,
393394
(_height - initialHeight) / 2,
@@ -629,7 +630,7 @@ class _CustomImageCropState extends State<CustomImageCrop>
629630
final imageHeight = _imageAsUIImage!.height;
630631
final pictureRecorder = ui.PictureRecorder();
631632
final canvas = Canvas(pictureRecorder);
632-
final onCropParams = caclulateOnCropParams(
633+
final onCropParams = calculateOnCropParams(
633634
cropPercentage: widget.cropPercentage,
634635
imageFit: widget.imageFit,
635636
imageHeight: imageHeight,
@@ -638,6 +639,7 @@ class _CustomImageCropState extends State<CustomImageCrop>
638639
screenWidth: _width,
639640
dataScale: data.scale,
640641
aspectRatio: (widget.ratio?.width ?? 1) / (widget.ratio?.height ?? 1),
642+
forceInsideCropArea: widget.forceInsideCropArea,
641643
);
642644
final clipPath = Path.from(_getPath(
643645
cropWidth: onCropParams.cropSizeWidth,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: custom_image_crop
22
description: An image cropper that is customizable. You can rotate, scale and translate either through gestures or a controller
3-
version: 0.1.0
3+
version: 0.1.1
44
homepage: https://github.com/icapps/flutter-custom-image-crop
55

66
environment:

0 commit comments

Comments
 (0)