From 03ec91a595bb8a005edba5c106e73338720fc28c Mon Sep 17 00:00:00 2001 From: misha Date: Wed, 15 Apr 2026 15:50:09 +0300 Subject: [PATCH] feat(image_compress_utility): improved compressing logic --- .../image_compress_utility.dart | 42 ++++--------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/lib/core/image_compressing/image_compress_utility.dart b/lib/core/image_compressing/image_compress_utility.dart index b9cbeb18..239824b4 100644 --- a/lib/core/image_compressing/image_compress_utility.dart +++ b/lib/core/image_compressing/image_compress_utility.dart @@ -3,7 +3,6 @@ import 'package:eatvio_app/core/app_initializer.dart'; import 'package:eatvio_app/core/app_config.dart'; import 'package:eatvio_app/core/locator_service.dart'; import 'package:flutter_image_compress/flutter_image_compress.dart'; -import 'package:image/image.dart' as img; import 'package:path/path.dart' as p; abstract class ImageCompressUtility { @@ -30,44 +29,21 @@ abstract class ImageCompressUtility { } static Future compressFile(File file) async { - // Skip compression in test environment to avoid hangs try { final appConfig = sl(); if (appConfig.buildType == AppBuildType.tests) { - // In tests, return the original file without compression return file; } - } catch (_) { - // If AppConfig is not available, proceed with compression + } catch (_) {} + + final compressedPath = '${p.withoutExtension(file.path)}_compressed.jpg'; + + final result = await FlutterImageCompress.compressAndGetFile(file.absolute.path, compressedPath, quality: 90, minHeight: 1080, minWidth: 1080); + + if (result == null) { + return file; } - final image = img.decodeImage(file.readAsBytesSync())!; - int width = image.width; - int height = image.height; - - if (width > height) { - if (width > 1080) { - height = (height * 1080) ~/ width; - width = 1080; - } - } else { - if (height > 1080) { - width = (width * 1080) ~/ height; - height = 1080; - } - } - - String filePathWithoutExtension = p.withoutExtension(file.path); - String compressedFilePath = '${filePathWithoutExtension}_compressed.jpg'; - - final result = await FlutterImageCompress.compressAndGetFile( - file.absolute.path, - compressedFilePath, - quality: 90, - minHeight: height, - minWidth: width, - ); - - return File(result!.path); + return File(result.path); } }