Open raulmabe opened 3 years ago
Any news?
Any fixes for this? It's a pretty good lib but it's increasing size by 4-5x
I think this may be a problem of the image
package https://github.com/brendan-duncan/image/issues/312
I suppose it's also because the image is encoded as PNG by default? https://github.com/chooyan-eng/crop_your_image/blob/main/lib/src/crop.dart#L687
Sorry for replying this late.
@luca-nardelli Thank you for your suggestion! Now it's clear what point should be checked. I'll try to fix this problem by determining the image format first and returning cropped data with the same format.
No problem, you can do that or also (even better) allow passing in a format + quality options, so that it's fully customizable
Any news?
I have opened a pull request (https://github.com/chooyan-eng/crop_your_image/pull/102) that would fix this issue, by exposing an encodeFunction
parameter.
As an example i tried setting it to encodeJpg(croppedImage, quality: 75)
(where encodeJpg
comes from package:image
) which reduced an image of 1700 KB to about 570 KB
@chooyan-eng Currently there is no formatDetectorImplemented by default. But you can try using something like this:
formatDetector: (data) {
final format = img.findFormatForData(data);
return switch (format) {
img.ImageFormat.png => ImageFormat.png,
img.ImageFormat.jpg => ImageFormat.jpeg,
img.ImageFormat.webp => ImageFormat.webp,
img.ImageFormat.bmp => ImageFormat.bmp,
img.ImageFormat.ico => ImageFormat.ico,
_ => ImageFormat.png,
};
},
With that proper ImageFormat will pass to _cropFunc(args)
=>cropper.call in crop.dart.
Then in ImageCropper there should be something similiar to this:
final encoder = switch (outputFormat) {
ImageFormat.bmp => encodeBmp,
ImageFormat.ico => encodeIco,
ImageFormat.jpeg => encodeJpg,
ImageFormat.png => encodePng,
_ => encodeJpg,
};
...
return function(
original,
encoder: encoder,
topLeft: topLeft,
size: Size(
bottomRight.dx - topLeft.dx,
bottomRight.dy - topLeft.dy,
),
);
And in _doCrop pass the encoder function.
return Uint8List.fromList(
encoder(
copyCrop(
original,
x: topLeft.dx.toInt(),
y: topLeft.dy.toInt(),
width: size.width.toInt(),
height: size.height.toInt(),
),
),
);
With that you have proper ImageFormat detection, which comes with normal cropped image size.
I have tested these on several images and when cropping it multiplies its size up to x3. Why is that? Am I doing something wrong?
Dart file
```dart class PictureCroppingPage extends StatefulWidget { const PictureCroppingPage({ Key? key, required this.picture, required this.onCropped, this.isCircular = false, this.aspectRatio, }) : super(key: key); final Uint8List picture; final Function(Uint8List) onCropped; final bool isCircular; final double? aspectRatio; @override _PictureCroppingPageState createState() => _PictureCroppingPageState(); } class _PictureCroppingPageState extends StateImages attached
![IMG_0003](https://user-images.githubusercontent.com/20211760/129740679-09346a67-07f4-43ba-b24a-dce838f3839b.JPG) ![IMG_0002](https://user-images.githubusercontent.com/20211760/129740663-fc4ce12e-74cb-46a5-a5f9-89f009fc22e0.JPG)