From 57615eb5d49cc00e57a114d198e7782dc7c1d2ee Mon Sep 17 00:00:00 2001 From: hamza aboshhiwa <42789638+Hamza-Shewa@users.noreply.github.com> Date: Sun, 17 Nov 2024 01:35:47 +0200 Subject: [PATCH] update dependencies --- .../lib/isolate_image_compress.dart | 2 +- .../lib/src/compress_format/compress_gif.dart | 26 +++++----- .../src/compress_format/compress_jpeg.dart | 48 ++++++++--------- .../lib/src/compress_format/compress_png.dart | 48 ++++++++--------- .../lib/src/compress_format/compress_tga.dart | 26 +++++----- .../lib/src/compress_utils.dart | 52 +++++++++---------- .../lib/src/constants/enums.dart | 13 +++-- .../lib/src/resize_utils.dart | 16 +++--- isolate_image_compress/pubspec.yaml | 6 +-- 9 files changed, 118 insertions(+), 119 deletions(-) diff --git a/isolate_image_compress/lib/isolate_image_compress.dart b/isolate_image_compress/lib/isolate_image_compress.dart index 081861f..2f68f3f 100644 --- a/isolate_image_compress/lib/isolate_image_compress.dart +++ b/isolate_image_compress/lib/isolate_image_compress.dart @@ -1,4 +1,4 @@ -library isolate_image_compress; +library; export 'src/compress_utils.dart'; export 'src/resize_utils.dart'; diff --git a/isolate_image_compress/lib/src/compress_format/compress_gif.dart b/isolate_image_compress/lib/src/compress_format/compress_gif.dart index bb3c74f..e70d745 100644 --- a/isolate_image_compress/lib/src/compress_format/compress_gif.dart +++ b/isolate_image_compress/lib/src/compress_format/compress_gif.dart @@ -18,28 +18,28 @@ Future compressGifImage(Uint8List data, return data; } - ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd; + ImageResolution? resolution = maxResolution ?? ImageResolution.uhd; - Image? _image = decodeImage(data); - if (_image == null) { + Image? image = decodeImage(data); + if (image == null) { return Uint8List(0); } else { - List? _data; + List? data0; do { - if (_resolution != null) { - _image = _image!.resizeWithResolution(_resolution); + if (resolution != null) { + image = image!.resizeWithResolution(resolution); print( - 'resizeWithResolution: ${_resolution.width} - ${_resolution.height}'); + 'resizeWithResolution: ${resolution.width} - ${resolution.height}'); } - _data = encodeGif(_image!); - print('encodeGif - length: ${_data.length}'); - if (_data.length < maxSize) { + data0 = encodeGif(image!); + print('encodeGif - length: ${data0.length}'); + if (data0.length < maxSize) { break; } - _resolution = _resolution?.prev(); - } while (_resolution != null); + resolution = resolution?.prev(); + } while (resolution != null); - return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0); + return data0.length < maxSize ? Uint8List.fromList(data0) : Uint8List(0); } } diff --git a/isolate_image_compress/lib/src/compress_format/compress_jpeg.dart b/isolate_image_compress/lib/src/compress_format/compress_jpeg.dart index f26a4c4..1e8513d 100644 --- a/isolate_image_compress/lib/src/compress_format/compress_jpeg.dart +++ b/isolate_image_compress/lib/src/compress_format/compress_jpeg.dart @@ -19,46 +19,46 @@ Future compressJpegImage(Uint8List data, } // quality: The JPEG quality, in the range [0, 100] where 100 is highest quality. - const _minQuality = 0; - const _maxQuality = 100; - const _step = 10; + const minQuality = 0; + const maxQuality = 100; + const step = 10; - ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd; + ImageResolution? resolution = maxResolution ?? ImageResolution.uhd; - Image? _image = decodeImage(data); - if (_image == null) { + Image? image = decodeImage(data); + if (image == null) { return Uint8List(0); } else { - List? _data; + List? data0; do { - if (_resolution != null) { - _image = _image!.resizeWithResolution(_resolution); + if (resolution != null) { + image = image!.resizeWithResolution(resolution); print( - 'resizeWithResolution: ${_resolution.width} - ${_resolution.height}'); + 'resizeWithResolution: ${resolution.width} - ${resolution.height}'); } - _data = encodeJpg(_image!, quality: _maxQuality); - print('encodeJpg - _maxQuality: ${_data.length}'); + data0 = encodeJpg(image!, quality: maxQuality); + print('encodeJpg - _maxQuality: ${data0.length}'); - if (_data.length > maxSize) { - _data = encodeJpg(_image, quality: _minQuality); - print('encodeJpg - _minQuality: ${_data.length}'); + if (data0.length > maxSize) { + data0 = encodeJpg(image, quality: minQuality); + print('encodeJpg - _minQuality: ${data0.length}'); - if (_data.length < maxSize) { - int _quality = _maxQuality; + if (data0.length < maxSize) { + int quality = maxQuality; do { - _quality -= _step; - _data = encodeJpg(_image, quality: _quality); - print('encodeJpg - _quality - $_quality: ${_data.length}'); - } while (_data.length > maxSize && _quality > _minQuality); + quality -= step; + data0 = encodeJpg(image, quality: quality); + print('encodeJpg - _quality - $quality: ${data0.length}'); + } while (data0.length > maxSize && quality > minQuality); break; } } - _resolution = _resolution?.prev(); - } while (_resolution != null); + resolution = resolution?.prev(); + } while (resolution != null); - return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0); + return data0.length < maxSize ? Uint8List.fromList(data0) : Uint8List(0); } } diff --git a/isolate_image_compress/lib/src/compress_format/compress_png.dart b/isolate_image_compress/lib/src/compress_format/compress_png.dart index 5aae7b2..b3469ec 100644 --- a/isolate_image_compress/lib/src/compress_format/compress_png.dart +++ b/isolate_image_compress/lib/src/compress_format/compress_png.dart @@ -19,45 +19,45 @@ Future compressPngImage(Uint8List data, } // level: The compression level, in the range [0, 9] where 9 is the most compressed. - const _minLevel = 0; - const _maxLevel = 9; - const _step = 1; + const minLevel = 0; + const maxLevel = 9; + const step = 1; - ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd; + ImageResolution? resolution = maxResolution ?? ImageResolution.uhd; - Image? _image = decodeImage(data); - if (_image == null) { + Image? image = decodeImage(data); + if (image == null) { return Uint8List(0); } else { - List? _data; + List? data0; do { - if (_resolution != null) { - _image = _image!.resizeWithResolution(_resolution); + if (resolution != null) { + image = image!.resizeWithResolution(resolution); print( - 'resizeWithResolution: ${_resolution.width} - ${_resolution.height}'); + 'resizeWithResolution: ${resolution.width} - ${resolution.height}'); } - _data = encodePng(_image!, level: _minLevel); - print('encodePNG - _minLevel: ${_data.length}'); + data0 = encodePng(image!, level: minLevel); + print('encodePNG - _minLevel: ${data0.length}'); - if (_data.length > maxSize) { - _data = encodePng(_image, level: _maxLevel); - print('encodePNG - _maxLevel: ${_data.length}'); + if (data0.length > maxSize) { + data0 = encodePng(image, level: maxLevel); + print('encodePNG - _maxLevel: ${data0.length}'); - if (_data.length < maxSize) { - int _level = _minLevel; + if (data0.length < maxSize) { + int level = minLevel; do { - _level += _step; - _data = encodePng(_image, level: _level); - print('encodePNG - _level - $_level: ${_data.length}'); - } while (_data.length > maxSize && _level < _maxLevel); + level += step; + data0 = encodePng(image, level: level); + print('encodePNG - _level - $level: ${data0.length}'); + } while (data0.length > maxSize && level < maxLevel); break; } } - _resolution = _resolution?.prev(); - } while (_resolution != null); + resolution = resolution?.prev(); + } while (resolution != null); - return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0); + return data0.length < maxSize ? Uint8List.fromList(data0) : Uint8List(0); } } diff --git a/isolate_image_compress/lib/src/compress_format/compress_tga.dart b/isolate_image_compress/lib/src/compress_format/compress_tga.dart index c6793a7..499d4ee 100644 --- a/isolate_image_compress/lib/src/compress_format/compress_tga.dart +++ b/isolate_image_compress/lib/src/compress_format/compress_tga.dart @@ -18,28 +18,28 @@ Future compressTgaImage(Uint8List data, return data; } - ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd; + ImageResolution? resolution = maxResolution ?? ImageResolution.uhd; - Image? _image = decodeImage(data); - if (_image == null) { + Image? image = decodeImage(data); + if (image == null) { return Uint8List(0); } else { - List? _data; + List? data0; do { - if (_resolution != null) { - _image = _image!.resizeWithResolution(_resolution); + if (resolution != null) { + image = image!.resizeWithResolution(resolution); print( - 'resizeWithResolution: ${_resolution.width} - ${_resolution.height}'); + 'resizeWithResolution: ${resolution.width} - ${resolution.height}'); } - _data = encodeTga(_image!); - print('encodeTga - length: ${_data.length}'); - if (_data.length < maxSize) { + data0 = encodeTga(image!); + print('encodeTga - length: ${data0.length}'); + if (data0.length < maxSize) { break; } - _resolution = _resolution?.prev(); - } while (_resolution != null); + resolution = resolution?.prev(); + } while (resolution != null); - return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0); + return data0.length < maxSize ? Uint8List.fromList(data0) : Uint8List(0); } } diff --git a/isolate_image_compress/lib/src/compress_utils.dart b/isolate_image_compress/lib/src/compress_utils.dart index 95e35a1..e569d0c 100644 --- a/isolate_image_compress/lib/src/compress_utils.dart +++ b/isolate_image_compress/lib/src/compress_utils.dart @@ -39,33 +39,33 @@ class CompressParams { } Future _compressImage(CompressParams params) async { - final _maxSize = params.maxSize; + final maxSize = params.maxSize; // read image data - final Uint8List _fileData = + final Uint8List fileData = params.imageData ?? params.image?.data ?? Uint8List(0); - if (_fileData.isEmpty || _maxSize == null || _fileData.length < _maxSize) { + if (fileData.isEmpty || maxSize == null || fileData.length < maxSize) { // not compression - return _fileData; + return fileData; } else { - final _maxResolution = params.maxResolution; + final maxResolution = params.maxResolution; - Decoder? _decoder = + Decoder? decoder = (params.format != null ? _getDecoder(params.format!) : null) ?? - findDecoderForData(_fileData); - if (_decoder is JpegDecoder) { - return compressJpegImage(_fileData, - maxSize: _maxSize, maxResolution: _maxResolution); - } else if (_decoder is PngDecoder) { - return compressPngImage(_fileData, - maxSize: _maxSize, maxResolution: _maxResolution); - } else if (_decoder is TgaDecoder) { - return compressTgaImage(_fileData, - maxSize: _maxSize, maxResolution: _maxResolution); - } else if (_decoder is GifDecoder) { - return compressGifImage(_fileData, - maxSize: _maxSize, maxResolution: _maxResolution); + findDecoderForData(fileData); + if (decoder is JpegDecoder) { + return compressJpegImage(fileData, + maxSize: maxSize, maxResolution: maxResolution); + } else if (decoder is PngDecoder) { + return compressPngImage(fileData, + maxSize: maxSize, maxResolution: maxResolution); + } else if (decoder is TgaDecoder) { + return compressTgaImage(fileData, + maxSize: maxSize, maxResolution: maxResolution); + } else if (decoder is GifDecoder) { + return compressGifImage(fileData, + maxSize: maxSize, maxResolution: maxResolution); } return Uint8List(0); @@ -74,7 +74,7 @@ Future _compressImage(CompressParams params) async { Decoder? _getDecoder(ImageFormat format) { switch (format) { - case ImageFormat.jpeg: + case ImageFormat.jpg: return JpegDecoder(); case ImageFormat.png: return PngDecoder(); @@ -99,12 +99,12 @@ extension CompressOnIsolateImage on IsolateImage { {int? maxSize, ImageResolution? maxResolution, ImageFormat? format}) async { - final CompressParams _params = CompressParams( + final CompressParams params = CompressParams( image: this, maxSize: maxSize, maxResolution: maxResolution, format: format); - return IsolateFlutter.createAndStart(_compressImage, _params, + return IsolateFlutter.createAndStart(_compressImage, params, debugLabel: 'isolate_image_compress'); } } @@ -117,12 +117,12 @@ extension CompressOnUint8List on Uint8List { /// - [format] - the image format you want to compress. (optional). Future compress( {int? maxSize, ImageResolution? resolution, ImageFormat? format}) async { - final CompressParams _params = CompressParams( + final CompressParams params = CompressParams( imageData: this, maxSize: maxSize, maxResolution: resolution, format: format); - return IsolateFlutter.createAndStart(_compressImage, _params, + return IsolateFlutter.createAndStart(_compressImage, params, debugLabel: 'isolate_image_compress'); } } @@ -135,12 +135,12 @@ extension CompressOnListInt on List { /// - [format] - the image format you want to compress. (optional). Future compress( {int? maxSize, ImageResolution? resolution, ImageFormat? format}) async { - final CompressParams _params = CompressParams( + final CompressParams params = CompressParams( imageData: Uint8List.fromList(this), maxSize: maxSize, maxResolution: resolution, format: format); - return IsolateFlutter.createAndStart(_compressImage, _params, + return IsolateFlutter.createAndStart(_compressImage, params, debugLabel: 'isolate_image_compress'); } } diff --git a/isolate_image_compress/lib/src/constants/enums.dart b/isolate_image_compress/lib/src/constants/enums.dart index 9e9ffae..a774379 100644 --- a/isolate_image_compress/lib/src/constants/enums.dart +++ b/isolate_image_compress/lib/src/constants/enums.dart @@ -1,5 +1,4 @@ enum ImageFromType { file, path } -enum ImageFormat { png, jpeg, gif, tga } class ImageResolution { final int width; @@ -28,17 +27,17 @@ class ImageResolution { static const List all = [sd, hd, fhd, qhd, uhd, fuhd]; ImageResolution? prev() { - final _index = all.indexOf(this); - if (_index > 0) { - return all[_index - 1]; + final index = all.indexOf(this); + if (index > 0) { + return all[index - 1]; } return null; } ImageResolution? next() { - final _index = all.indexOf(this); - if (_index < all.length - 1) { - return all[_index + 1]; + final index = all.indexOf(this); + if (index < all.length - 1) { + return all[index + 1]; } return null; } diff --git a/isolate_image_compress/lib/src/resize_utils.dart b/isolate_image_compress/lib/src/resize_utils.dart index 346d2a2..473b12f 100644 --- a/isolate_image_compress/lib/src/resize_utils.dart +++ b/isolate_image_compress/lib/src/resize_utils.dart @@ -6,18 +6,18 @@ import 'package:isolate_image_compress/src/entity/isolate_image.dart'; extension ResizeOnImage on Image { /// Resize image with resolution Image resizeWithResolution(ImageResolution resolution) { - int? _newWidth, _newHeight; + int? newWidth, newHeight; if (width < height) { if (height > resolution.height) { - _newHeight = resolution.height; + newHeight = resolution.height; } } else { if (width > resolution.width) { - _newWidth = resolution.width; + newWidth = resolution.width; } } - if (_newWidth != null || _newHeight != null) { - return copyResize(this, width: _newWidth, height: _newHeight); + if (newWidth != null || newHeight != null) { + return copyResize(this, width: newWidth, height: newHeight); } return this; @@ -28,9 +28,9 @@ extension ResizeOnIsolateImage on IsolateImage { /// Resize image with resolution Image? resizeWithResolution(ImageResolution resolution) { if (data?.isNotEmpty == true) { - final _image = decodeImage(data!); - if (_image != null) { - return _image.resizeWithResolution(resolution); + final image = decodeImage(data!); + if (image != null) { + return image.resizeWithResolution(resolution); } } return null; diff --git a/isolate_image_compress/pubspec.yaml b/isolate_image_compress/pubspec.yaml index 0b1e091..f6bae89 100644 --- a/isolate_image_compress/pubspec.yaml +++ b/isolate_image_compress/pubspec.yaml @@ -5,16 +5,16 @@ homepage: https://github.com/thongdn-it/isolate_flutter/tree/master/isolate_imag repository: https://github.com/thongdn-it/isolate_flutter/tree/master/isolate_image_compress environment: - sdk: '>=2.12.0 <3.0.0' + sdk: '>=3.3.1' dependencies: flutter: sdk: flutter isolate_flutter: ^3.0.0 - image: ^4.0.5 + image: ^4.3.0 dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^1.0.0 + flutter_lints: ^5.0.0