Hello i am using image_picker plugin on flutter and with it i can change the quality of an image from 100 percent to 1. with image quality property. Some of my uploaded images are 500 kb to 10 mb so some needs compressing and some dont.
So is it possible to check the 100% quality version and if it is less size than 2 mb it stays 100% and we upload the image.
if it is bigger than 2mb drop the quality to 75%.
check the size < 2 mb upload it or >2mb drop to 50 quality.
try it on 25% and 10% and 1% if its still not less than 2mb on 1 percent quality just dont upload it.
there must be a way to do this like even on instagram they try to save the photos on full size but i dont think so that they will still save it on full size if it is bigger than 100mb or something like that right?
and this plugin opens a weird last 40 images page? on the pick file screen why doesnt it open the default gallery app although i use the source: ImageSource.gallery property ?
CodePudding user response:
First you can get the image size by following code:
final bytes = image.readAsBytesSync().lengthInBytes;
final kb = bytes / 1024;
final mb = kb / 1024;
Then you can implement switch cases of if-else statements to implement image compressing functionality.
Also there's a package called flutter_image_compress
which can compress your image after you have selected it.
Also they have provided various examples for multiple file formats
CodePudding user response:
ImagePicker()
.pickImage(
source: useCamera ? ImageSource.camera : ImageSource.gallery,
imageQuality: 60)
Hope it will help.
CodePudding user response:
What facebook and instagram have been doing since forever is use a lot of pixels - use biggest size possible - and set the quality parameter to 0.
Looks good.
Of course you can manually re-encode depending on size, you would call the picker with quality of 100, then work on the resulting image, re-encoding it until it fits under your limit. It's pretty CPU intensive to do so though.