i'm asking the user to crop any image he choose from the gallery with the image cropper with aspect ratio 1/1 then i realize that image package can also crop without even asking the user but i don't know how to calculate the aspect ratio if it is 1/1 and how to pass these values to the image.copycrop(int x, int y, int height,int width) !
i was using this code to crop
final croppedImage = await ImageCropper().cropImage(
sourcePath: images!.path,
cropStyle: CropStyle.rectangle,
compressQuality: 100,
compressFormat: ImageCompressFormat.jpg,
aspectRatio: const CropAspectRatio(ratioX: 0.8, ratioY: 1.0),
now i figured out that i can crop with image package but i don't know how to insert the values like the cropasepectratio(0.8,1.0)
var decodedImage = await decodeImageFromList(File(images!.path).readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height);
final imageBytes =
decodeImage(File(images!.path).readAsBytesSync())!;
img.Image cropOne = img.copyCrop(
imageBytes,
100,
100,
decodedImage.width,
decodedImage.height,
);
File(images!.path).writeAsBytes(encodePng(cropOne));
This is the solution to crop and keep the aspect ratio of 1/1. It crops the image exactly in the middle.
var decodedImage = await decodeImageFromList(File(images!.path).readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height);
var cropSize = min(decodedImage.width, decodedImage.height);
int offsetX = (decodedImage.width - min(decodedImage.width, decodedImage.height)) ~/ 2;
int offsetY = (decodedImage.height - min(decodedImage.width, decodedImage.height)) ~/ 2;
final imageBytes = decodeImage(File(images!.path).readAsBytesSync())!;
img.Image cropOne = img.copyCrop(
imageBytes,
offsetX,
offsetY,
cropSize,
cropSize,
);
print(cropOne.height);
print(cropOne.width);
File(images!.path).writeAsBytes(encodePng(cropOne));