Skip to content

Commit

Permalink
Merge pull request #4 from thongdn-it/isolate_image_compress
Browse files Browse the repository at this point in the history
Publish isolate_image_compress version 1.0.0
  • Loading branch information
thongdn-it authored Apr 6, 2022
2 parents 03dca99 + 9845160 commit a8435f4
Show file tree
Hide file tree
Showing 20 changed files with 674 additions and 0 deletions.
29 changes: 29 additions & 0 deletions isolate_image_compress/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions isolate_image_compress/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 097d3313d8e2c7f901932d63e537c1acefb87800
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions isolate_image_compress/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# [1.0.0] - 06/04/2022

- Initial package.
21 changes: 21 additions & 0 deletions isolate_image_compress/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Thong Dang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions isolate_image_compress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# IsolateImageCompress

[![Pub][pub_v_image_url]][pub_url]

IsolateImageCompress is a package to compress and resize the images in isolate ([IsolateFlutter][isolate_flutter_pub_url]).

## Usage

### Compress

- Compress with file path

```dart
void compressImage(String path) async {
final _image = IsolateImage.path(path);
print('isolate_image_compress begin - : ${_image.data.length}');
final _data = await _image.compress(maxSize: 1 * 1024 * 1024); // 1 MB
print('isolate_image_compress end - : ${_data.length}');
}
```

- Compress with file data (`Uint8List` or `List<int>`)

```dart
void compressImage(Uint8List fileData) async {
print('isolate_image_compress begin - : ${fileData.length}');
final _data = fileData.compress(maxSize: 1 * 1024 * 1024); // 1 MB
print('isolate_image_compress end - : ${_data.length}');
}
```

### Resize

```dart
void resizeImage(String path) {
final _image = IsolateImage.path(path);
_image.resizeWithResolution(ImageResolution.fhd);
}
```

## Author

IsolateImageCompress is developed by Thong Dang. You can contact me at thongdn.it@gmail.com

If you like my project, you can support me [![Buy Me A Coffee][buy_me_a_coffee_image_url]][buy_me_a_coffee_url] or star (like) for it.

Thank you! ❤️

[//]: # 'reference links'
[isolate_flutter_pub_url]: https://pub.dev/packages/isolate_flutter
[pub_url]: https://pub.dev/packages/isolate_image_compress
[pub_v_image_url]: https://img.shields.io/pub/v/isolate_image_compress.svg
[buy_me_a_coffee_image_url]: https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png
[buy_me_a_coffee_url]: https://www.buymeacoffee.com/thongdn.it
4 changes: 4 additions & 0 deletions isolate_image_compress/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
35 changes: 35 additions & 0 deletions isolate_image_compress/example/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```dart
import 'package:isolate_image_compress/isolate_image_compress.dart';
```

## Compress

- Compress with file path

```dart
void compressImage(String path) async {
final _image = IsolateImage.path(path);
print('isolate_image_compress begin - : ${_image.data.length}');
final _data = await _image.compress(maxSize: 1 * 1024 * 1024); // 1 MB
print('isolate_image_compress end - : ${_data.length}');
}
```

- Compress with file data (Uint8List)

```dart
void compressImage(Uint8List fileData) async {
print('isolate_image_compress begin - : ${fileData.length}');
final _data = fileData.compress(maxSize: 1 * 1024 * 1024); // 1 MB
print('isolate_image_compress end - : ${_data.length}');
}
```

## Resize

```dart
void resizeImage(String path) {
final _image = IsolateImage.path(path);
_image.resizeWithResolution(ImageResolution.fhd);
}
```
7 changes: 7 additions & 0 deletions isolate_image_compress/lib/isolate_image_compress.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library isolate_image_compress;

export 'src/compress_utils.dart';
export 'src/resize_utils.dart';
export 'src/constants/enums.dart';
export 'src/entity/isolate_image.dart';
export 'src/compress_format/index.dart';
45 changes: 45 additions & 0 deletions isolate_image_compress/lib/src/compress_format/compress_gif.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:typed_data';

import 'package:image/image.dart';

import 'package:isolate_image_compress/src/log_utils.dart';
import 'package:isolate_image_compress/src/resize_utils.dart';
import 'package:isolate_image_compress/src/constants/enums.dart';

/// Compress Gif Image - return empty(*Uint8List(0)*) if image can't be compressed.
///
/// Params:
/// - [data] The image data to compress.
/// - [maxSize] limit file size you want to compress (Bytes). If it is null, return [data].
/// - [maxResolution] limit image resolution you want to compress ([ImageResolution]). Default is [ImageResolution.uhd].
Future<Uint8List> compressGifImage(Uint8List data,
{int? maxSize, ImageResolution? maxResolution}) async {
if (maxSize == null) {
return data;
}

ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd;

Image? _image = decodeImage(data);
if (_image == null) {
return Uint8List(0);
} else {
List<int>? _data;
do {
if (_resolution != null) {
_image = _image!.resizeWithResolution(_resolution);
print(
'resizeWithResolution: ${_resolution.width} - ${_resolution.height}');
}
_data = encodeGif(_image!);
print('encodeGif - length: ${_data.length}');
if (_data.length < maxSize) {
break;
}

_resolution = _resolution?.prev();
} while (_resolution != null);

return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0);
}
}
64 changes: 64 additions & 0 deletions isolate_image_compress/lib/src/compress_format/compress_jpeg.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'dart:typed_data';

import 'package:image/image.dart';

import 'package:isolate_image_compress/src/log_utils.dart';
import 'package:isolate_image_compress/src/resize_utils.dart';
import 'package:isolate_image_compress/src/constants/enums.dart';

/// Compress Jpeg Image - return empty(*Uint8List(0)*) if image can't be compressed.
///
/// Params:
/// - [data] The image data to compress.
/// - [maxSize] limit file size you want to compress (Bytes). If it is null, return [data].
/// - [maxResolution] limit image resolution you want to compress ([ImageResolution]). Default is [ImageResolution.uhd].
Future<Uint8List> compressJpegImage(Uint8List data,
{int? maxSize, ImageResolution? maxResolution}) async {
if (maxSize == null) {
return data;
}

// quality: The JPEG quality, in the range [0, 100] where 100 is highest quality.
const _minQuality = 0;
const _maxQuality = 100;
const _step = 10;

ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd;

Image? _image = decodeImage(data);
if (_image == null) {
return Uint8List(0);
} else {
List<int>? _data;
do {
if (_resolution != null) {
_image = _image!.resizeWithResolution(_resolution);
print(
'resizeWithResolution: ${_resolution.width} - ${_resolution.height}');
}

_data = encodeJpg(_image!, quality: _maxQuality);
print('encodeJpg - _maxQuality: ${_data.length}');

if (_data.length > maxSize) {
_data = encodeJpg(_image, quality: _minQuality);
print('encodeJpg - _minQuality: ${_data.length}');

if (_data.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);

break;
}
}

_resolution = _resolution?.prev();
} while (_resolution != null);

return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0);
}
}
63 changes: 63 additions & 0 deletions isolate_image_compress/lib/src/compress_format/compress_png.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:typed_data';

import 'package:image/image.dart';

import 'package:isolate_image_compress/src/log_utils.dart';
import 'package:isolate_image_compress/src/resize_utils.dart';
import 'package:isolate_image_compress/src/constants/enums.dart';

/// Compress PNG Image - return empty(*Uint8List(0)*) if image can't be compressed.
///
/// Params:
/// - [data] The image data to compress.
/// - [maxSize] limit file size you want to compress (Bytes). If it is null, return [data].
/// - [maxResolution] limit image resolution you want to compress ([ImageResolution]). Default is [ImageResolution.uhd].
Future<Uint8List> compressPngImage(Uint8List data,
{int? maxSize, ImageResolution? maxResolution}) async {
if (maxSize == null) {
return 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;

ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd;

Image? _image = decodeImage(data);
if (_image == null) {
return Uint8List(0);
} else {
List<int>? _data;
do {
if (_resolution != null) {
_image = _image!.resizeWithResolution(_resolution);
print(
'resizeWithResolution: ${_resolution.width} - ${_resolution.height}');
}

_data = encodePng(_image!, level: _minLevel);
print('encodePNG - _minLevel: ${_data.length}');

if (_data.length > maxSize) {
_data = encodePng(_image, level: _maxLevel);
print('encodePNG - _maxLevel: ${_data.length}');

if (_data.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);

break;
}
}
_resolution = _resolution?.prev();
} while (_resolution != null);

return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0);
}
}
45 changes: 45 additions & 0 deletions isolate_image_compress/lib/src/compress_format/compress_tga.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:typed_data';

import 'package:image/image.dart';

import 'package:isolate_image_compress/src/log_utils.dart';
import 'package:isolate_image_compress/src/resize_utils.dart';
import 'package:isolate_image_compress/src/constants/enums.dart';

/// Compress Tga Image - return empty(*Uint8List(0)*) if image can't be compressed.
///
/// Params:
/// - [data] The image data to compress.
/// - [maxSize] limit file size you want to compress (Bytes). If it is null, return [data].
/// - [maxResolution] limit image resolution you want to compress ([ImageResolution]). Default is [ImageResolution.uhd].
Future<Uint8List> compressTgaImage(Uint8List data,
{int? maxSize, ImageResolution? maxResolution}) async {
if (maxSize == null) {
return data;
}

ImageResolution? _resolution = maxResolution ?? ImageResolution.uhd;

Image? _image = decodeImage(data);
if (_image == null) {
return Uint8List(0);
} else {
List<int>? _data;
do {
if (_resolution != null) {
_image = _image!.resizeWithResolution(_resolution);
print(
'resizeWithResolution: ${_resolution.width} - ${_resolution.height}');
}
_data = encodeTga(_image!);
print('encodeTga - length: ${_data.length}');
if (_data.length < maxSize) {
break;
}

_resolution = _resolution?.prev();
} while (_resolution != null);

return _data.length < maxSize ? Uint8List.fromList(_data) : Uint8List(0);
}
}
4 changes: 4 additions & 0 deletions isolate_image_compress/lib/src/compress_format/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'compress_gif.dart';
export 'compress_jpeg.dart';
export 'compress_png.dart';
export 'compress_tga.dart';
Loading

0 comments on commit a8435f4

Please sign in to comment.