fix(storage): use separate attempt counters for each compression pass
Co-authored-by: derkauzigekoala <79001016+derkauzigekoala@users.noreply.github.com>
This commit is contained in:
parent
a68a1b1f0e
commit
bacc4292da
1 changed files with 6 additions and 5 deletions
|
|
@ -123,26 +123,27 @@ class StorageService {
|
||||||
: decoded;
|
: decoded;
|
||||||
|
|
||||||
var quality = 85;
|
var quality = 85;
|
||||||
var attempts = 0;
|
|
||||||
Uint8List out = Uint8List.fromList(img.encodeJpg(working, quality: quality));
|
Uint8List out = Uint8List.fromList(img.encodeJpg(working, quality: quality));
|
||||||
|
|
||||||
// First pass: reduce JPEG quality.
|
// First pass: reduce JPEG quality.
|
||||||
|
var qualityAttempts = 0;
|
||||||
while (out.lengthInBytes > _maxImageBytes &&
|
while (out.lengthInBytes > _maxImageBytes &&
|
||||||
quality > 35 &&
|
quality > 35 &&
|
||||||
attempts < _maxCompressionAttempts) {
|
qualityAttempts < _maxCompressionAttempts) {
|
||||||
quality -= 5;
|
quality -= 5;
|
||||||
out = Uint8List.fromList(img.encodeJpg(working, quality: quality));
|
out = Uint8List.fromList(img.encodeJpg(working, quality: quality));
|
||||||
attempts += 1;
|
qualityAttempts += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second pass: reduce dimensions progressively if still above the limit.
|
// Second pass: reduce dimensions progressively if still above the limit.
|
||||||
|
var dimensionAttempts = 0;
|
||||||
while (out.lengthInBytes > _maxImageBytes &&
|
while (out.lengthInBytes > _maxImageBytes &&
|
||||||
working.width > 320 &&
|
working.width > 320 &&
|
||||||
attempts < _maxCompressionAttempts) {
|
dimensionAttempts < _maxCompressionAttempts) {
|
||||||
final nextWidth = (working.width * 0.85).round();
|
final nextWidth = (working.width * 0.85).round();
|
||||||
working = img.copyResize(working, width: nextWidth);
|
working = img.copyResize(working, width: nextWidth);
|
||||||
out = Uint8List.fromList(img.encodeJpg(working, quality: quality));
|
out = Uint8List.fromList(img.encodeJpg(working, quality: quality));
|
||||||
attempts += 1;
|
dimensionAttempts += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out.lengthInBytes > _maxImageBytes) {
|
if (out.lengthInBytes > _maxImageBytes) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue