Merge pull request #9 from DieLustigenTierwesen/copilot/sub-pr-6-another-one
fix: use separate attempt counters for each compression pass in _compressImage
This commit is contained in:
commit
728cd582db
1 changed files with 6 additions and 5 deletions
|
|
@ -129,26 +129,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