Skip to content

Commit

Permalink
Merge pull request #145 from 4Ailen/feat/#144_Image-compress
Browse files Browse the repository at this point in the history
Feat/#144 image compression
  • Loading branch information
urlotus authored Sep 9, 2023
2 parents c598c82 + 52a40df commit 654705b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lib/apis/apis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import '../models/message_model.dart';
import '../models/notification_article_model.dart';
import '../models/partner_model.dart';
import '../models/screenArgument.dart';
import '../util/image_util.dart';

class APIs {
static final storage = FlutterSecureStorage();
Expand Down Expand Up @@ -732,8 +733,7 @@ class APIs {
jwtToken = json.decode(jwtToken!)['data']['accessToken'];

// MultipartFile로 변환

var profileImage = await http.MultipartFile.fromPath(
var profileImage = await ImageUtil.compressImageToMultipartFile(
'profileImage',
profileImageFile.path,
);
Expand Down
32 changes: 32 additions & 0 deletions lib/util/image_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:http/http.dart' as http;
import 'package:flutter_image_compress/flutter_image_compress.dart';

class ImageUtil {
static Future<http.MultipartFile> compressImageToMultipartFile(String fieldName, String imagePath) async {
const int maxSizeInBytes = 1 * 1000000; // 1MB
double compressionQuality = 1.0;

// 이미지 압축 함수
Future<List<int>> compressImage(double quality) async {
return (await FlutterImageCompress.compressWithFile(
imagePath,
quality: (quality * 100).toInt(),
)) as List<int>;
}

List<int> imageData = await compressImage(1.0);

// 이미지 용량이 초과될 경우, 반복해서 압축
while (imageData.length > maxSizeInBytes && compressionQuality > 0) {
imageData = await compressImage(compressionQuality);
compressionQuality -= 0.1;
}

// 압축된 이미지 파일 반환
return http.MultipartFile.fromBytes(
fieldName,
imageData,
filename: 'compressed_image.jpg',
);
}
}
82 changes: 53 additions & 29 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ packages:
dependency: "direct main"
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0
url: "https://pub.dev"
source: hosted
version: "2.11.0"
version: "2.10.0"
blobs:
dependency: "direct main"
description:
Expand All @@ -61,10 +61,10 @@ packages:
dependency: transitive
description:
name: characters
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.2.1"
chips_choice:
dependency: "direct main"
description:
Expand All @@ -85,10 +85,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
url: "https://pub.dev"
source: hosted
version: "1.17.2"
version: "1.17.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -278,6 +278,38 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_image_compress:
dependency: "direct main"
description:
name: flutter_image_compress
sha256: "2725cce5c58fdeaf1db8f4203688228bb67e3523a66305ccaa6f99071beb6dc2"
url: "https://pub.dev"
source: hosted
version: "2.0.4"
flutter_image_compress_common:
dependency: transitive
description:
name: flutter_image_compress_common
sha256: "8e7299afe109dc4b97fda34bf0f4967cc1fc10bc8050c374d449cab262d095b3"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
flutter_image_compress_platform_interface:
dependency: transitive
description:
name: flutter_image_compress_platform_interface
sha256: "3c7e86da7540b1adfa919b461885a41a018d4a26544d0fcbeaa769f6542e603d"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
flutter_image_compress_web:
dependency: transitive
description:
name: flutter_image_compress_web
sha256: e879189dc7f246dcf8f06c07ee849231341508bf51e8ed7d5dcbe778ddde0e81
url: "https://pub.dev"
source: hosted
version: "0.1.3+1"
flutter_inset_box_shadow:
dependency: "direct main"
description:
Expand Down Expand Up @@ -562,10 +594,10 @@ packages:
dependency: transitive
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
sha256: "910f85bce16fb5c6f614e117efa303e85a1731bb0081edf3604a2ae6e9a3cc91"
url: "https://pub.dev"
source: hosted
version: "0.18.1"
version: "0.17.0"
js:
dependency: transitive
description:
Expand All @@ -586,26 +618,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.13"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.2.0"
meta:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.8.0"
multi_image_picker_view:
dependency: "direct main"
description:
Expand Down Expand Up @@ -634,10 +666,10 @@ packages:
dependency: "direct main"
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.8.2"
path_parsing:
dependency: transitive
description:
Expand Down Expand Up @@ -855,10 +887,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.9.1"
sqflite:
dependency: "direct main"
description:
Expand Down Expand Up @@ -943,10 +975,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.4.16"
timezone:
dependency: transitive
description:
Expand Down Expand Up @@ -1083,14 +1115,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
web_socket_channel:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1140,5 +1164,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
dart: ">=2.19.2 <3.0.0"
flutter: ">=3.7.0-0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies:
url_launcher: ^6.1.11
flutter_screenutil: ^5.7.0
permission_handler: ^10.4.3
flutter_image_compress: ^2.0.4
# =======


Expand Down

0 comments on commit 654705b

Please sign in to comment.