{"id":13551256,"url":"https://github.com/fluttercandies/flutter_image_compress","last_synced_at":"2025-10-23T02:38:57.177Z","repository":{"id":38977528,"uuid":"147828290","full_name":"fluttercandies/flutter_image_compress","owner":"fluttercandies","description":"flutter image compress","archived":false,"fork":false,"pushed_at":"2025-02-06T14:33:37.000Z","size":11555,"stargazers_count":667,"open_issues_count":102,"forks_count":235,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-13T20:17:56.988Z","etag":null,"topics":["compress","flutter"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/flutter_image_compress","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fluttercandies.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-07T13:38:20.000Z","updated_at":"2025-04-04T04:07:26.000Z","dependencies_parsed_at":"2023-10-16T18:31:20.846Z","dependency_job_id":"fa4c1e1a-ec83-45ba-938e-3524112bdbce","html_url":"https://github.com/fluttercandies/flutter_image_compress","commit_stats":{"total_commits":223,"total_committers":33,"mean_commits":6.757575757575758,"dds":0.2600896860986547,"last_synced_commit":"1279b3e61b0cffd668fdedf30f0b6c20923cebd0"},"previous_names":["openflutter/flutter_image_compress"],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercandies%2Fflutter_image_compress","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercandies%2Fflutter_image_compress/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercandies%2Fflutter_image_compress/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fluttercandies%2Fflutter_image_compress/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fluttercandies","download_url":"https://codeload.github.com/fluttercandies/flutter_image_compress/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248774962,"owners_count":21159534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compress","flutter"],"created_at":"2024-08-01T12:01:45.126Z","updated_at":"2025-10-23T02:38:55.069Z","avatar_url":"https://github.com/fluttercandies.png","language":"Dart","funding_links":[],"categories":["Dart"],"sub_categories":[],"readme":"# flutter_image_compress\n\n[![ImageCompress](https://img.shields.io/badge/fluttercandies-ImageCompress-blue.svg)](https://github.com/fluttercandies/flutter_image_compress)\n[![pub package](https://img.shields.io/pub/v/flutter_image_compress.svg)](https://pub.dartlang.org/packages/flutter_image_compress)\n[![GitHub license](https://img.shields.io/github/license/fluttercandies/flutter_image_compress?style=flat-square)](https://github.com/fluttercandies/flutter_image_compress/blob/main/LICENSE)\n[![GitHub stars](https://img.shields.io/github/stars/fluttercandies/flutter_image_compress.svg?style=social\u0026label=Stars)](https://github.com/fluttercandies/flutter_image_compress)\n[![Awesome Flutter](https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true\u0026style=flat-square)](https://stackoverflow.com/questions/tagged/flutter?sort=votes)\n[![FlutterCandies](https://pub.idqqimg.com/wpa/images/group.png)](https://jq.qq.com/?_wv=1027\u0026k=5bcc0gy)\n\nCompresses image as native plugin (Obj-C/Kotlin). This library works on Android, iOS, macOS, Web, OpenHarmony.\n\n- [flutter\\_image\\_compress](#flutter_image_compress)\n  - [Why don't you use dart to do it](#why-dont-you-use-dart-to-do-it)\n  - [Platform Features](#platform-features)\n  - [Usage](#usage)\n  - [About common params](#about-common-params)\n    - [minWidth and minHeight](#minwidth-and-minheight)\n    - [rotate](#rotate)\n    - [autoCorrectionAngle](#autocorrectionangle)\n    - [quality](#quality)\n    - [format](#format)\n      - [Webp](#webp)\n      - [HEIF(Heic)](#heifheic)\n        - [Heif for iOS](#heif-for-ios)\n        - [Heif for Android](#heif-for-android)\n    - [inSampleSize](#insamplesize)\n    - [keepExif](#keepexif)\n  - [Result](#result)\n    - [About `List\u003cint\u003e` and `Uint8List`](#about-listint-and-uint8list)\n  - [Runtime Error](#runtime-error)\n  - [Android](#android)\n  - [Troubleshooting](#troubleshooting)\n    - [Compressing returns `null`](#compressing-returns-null)\n  - [About EXIF information](#about-exif-information)\n  - [Web](#web)\n  - [About macOS](#about-macos)\n  - [OpenHarmony](#openharmony)\n\n## Why don't you use dart to do it\n\nQ：Dart already has image compression libraries. Why use native?\n\nA：For unknown reasons, image compression in Dart language is not efficient,\neven in release version. Using isolate does not solve the problem.\n\n## Platform Features\n\n| Feature                    | Android |  iOS  |           Web           | macOS | OpenHarmony |\n| :------------------------- | :-----: | :---: | :---------------------: | :---: | :-------: |\n| method: compressWithList   |    ✅    |   ✅   |            ✅            |   ✅   |     ✅     |\n| method: compressAssetImage |    ✅    |   ✅   |            ✅            |   ✅   |     ✅     |\n| method: compressWithFile   |    ✅    |   ✅   |            ❌            |   ✅   |     ✅     |\n| method: compressAndGetFile |    ✅    |   ✅   |            ❌            |   ✅   |     ✅     |\n| format: jpeg               |    ✅    |   ✅   |            ✅            |   ✅   |     ✅     |\n| format: png                |    ✅    |   ✅   |            ✅            |   ✅   |     ✅     |\n| format: webp               |    ✅    |   ✅   | [🌐][webp-compatibility] |   ❌   |     ✅     |\n| format: heic               |    ✅    |   ✅   |            ❌            |   ✅   |     ✅     |\n| param: quality             |    ✅    |   ✅   | [🌐][webp-compatibility] |   ✅   |     ✅     |\n| param: rotate              |    ✅    |   ✅   |            ❌            |   ✅   |     ✅     |\n| param: keepExif            |    ✅    |   ✅   |            ❌            |   ✅   |     ❌     |\n\n[webp-compatibility]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#browser_compatibility \"Browser support\"\n\n\n## Usage\n\nSee the [![pub](https://img.shields.io/pub/v/flutter_image_compress.svg)](https://pub.dev/packages/flutter_image_compress/versions) version.\n\n```yaml\ndependencies:\n  flutter_image_compress: \u003clatest_version\u003e\n```\n\nor run this command:\n\n```bash\nflutter pub add flutter_image_compress\n```\n\nimport the package in your code:\n\n```dart\nimport 'package:flutter_image_compress/flutter_image_compress.dart';\n```\n\nUse as:\n\n[See full example](https://github.com/fluttercandies/flutter_image_compress/blob/main/packages/flutter_image_compress/example/lib/main.dart)\n\nThere are several ways to use the library api.\n\n```dart\n\n  // 1. compress file and get Uint8List\n  Future\u003cUint8List\u003e testCompressFile(File file) async {\n    var result = await FlutterImageCompress.compressWithFile(\n      file.absolute.path,\n      minWidth: 2300,\n      minHeight: 1500,\n      quality: 94,\n      rotate: 90,\n    );\n    print(file.lengthSync());\n    print(result.length);\n    return result;\n  }\n\n  // 2. compress file and get file.\n  Future\u003cFile\u003e testCompressAndGetFile(File file, String targetPath) async {\n    var result = await FlutterImageCompress.compressAndGetFile(\n        file.absolute.path, targetPath,\n        quality: 88,\n        rotate: 180,\n      );\n\n    print(file.lengthSync());\n    print(result.lengthSync());\n\n    return result;\n  }\n\n  // 3. compress asset and get Uint8List.\n  Future\u003cUint8List\u003e testCompressAsset(String assetName) async {\n    var list = await FlutterImageCompress.compressAssetImage(\n      assetName,\n      minHeight: 1920,\n      minWidth: 1080,\n      quality: 96,\n      rotate: 180,\n    );\n\n    return list;\n  }\n\n  // 4. compress Uint8List and get another Uint8List.\n  Future\u003cUint8List\u003e testComporessList(Uint8List list) async {\n    var result = await FlutterImageCompress.compressWithList(\n      list,\n      minHeight: 1920,\n      minWidth: 1080,\n      quality: 96,\n      rotate: 135,\n    );\n    print(list.length);\n    print(result.length);\n    return result;\n  }\n```\n\n## About common params\n\n### minWidth and minHeight\n\n`minWidth` and `minHeight` are constraints on image scaling.\n\nFor example, a 4000\\*2000 image, `minWidth` set to 1920,\n`minHeight` set to 1080, the calculation is as follows:\n\n```dart\n// Using dart as an example, the actual implementation is Kotlin or OC.\nimport 'dart:math' as math;\n\nvoid main() {\n  var scale = calcScale(\n    srcWidth: 4000,\n    srcHeight: 2000,\n    minWidth: 1920,\n    minHeight: 1080,\n  );\n\n  print(\"scale = $scale\"); // scale = 1.8518518518518519\n  print(\"target width = ${4000 / scale}, height = ${2000 / scale}\"); // target width = 2160.0, height = 1080.0\n}\n\ndouble calcScale({\n  double srcWidth,\n  double srcHeight,\n  double minWidth,\n  double minHeight,\n}) {\n  var scaleW = srcWidth / minWidth;\n  var scaleH = srcHeight / minHeight;\n  var scale = math.max(1.0, math.min(scaleW, scaleH));\n  return scale;\n}\n```\n\nIf your image width is smaller than `minWidth` or height smaller than `minHeight`,\nscale will be 1, that is, the size will not change.\n\n### rotate\n\nIf you need to rotate the picture, use this parameter.\n\n### autoCorrectionAngle\n\nThis property only exists in the version after 0.5.0.\n\nAnd for historical reasons, there may be conflicts with rotate attributes,\nwhich need to be self-corrected.\n\nModify rotate to 0 or autoCorrectionAngle to false.\n\n### quality\n\nQuality of target image.\n\nIf `format` is png, the param will be ignored in iOS.\n\n### format\n\nSupports jpeg or png, default is jpeg.\n\nThe format class sign `enum CompressFormat`.\n\nHeif and webp Partially supported.\n\n#### Webp\n\nSupport android by the system api (speed very nice).\nThe library also supports iOS. However, we're using\n[third-party libraries](https://github.com/SDWebImage/SDWebImageWebPCoder),\nit is not recommended due to encoding speed.\nIn the future, `libwebp` by google (C/C++) may be used to do coding work,\nbypassing other three-party libraries, but there are no plan for that currently.\n\n#### HEIF(Heic)\n\n##### Heif for iOS\n\nOnly support iOS 11+.\n\n##### Heif for Android\n\nUse [HeifWriter][] for the implementation.\n\nOnly support API 28+.\n\nAnd may require hardware encoder support,\ndoes not guarantee that all devices _above_ API 28 are available.\n\n[heifwriter]: https://developer.android.com/reference/androidx/heifwriter/HeifWriter.html\n\n### inSampleSize\n\nThe param is only support android.\n\nFor a description of this parameter, see the [Android official website](https://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize).\n\n### keepExif\n\nIf this parameter is true, EXIF information is saved in the compressed result.\n\nAttention should be paid to the following points:\n\n1. Default value is false.\n2. Even if set to true, the direction attribute is not included.\n3. Only support jpg format, PNG format does not support.\n\n## Result\n\nThe result of returning a List collection will not have null, but will always be an empty array.\n\nThe returned file may be null. In addition, please decide for yourself whether the file exists.\n\n### About `List\u003cint\u003e` and `Uint8List`\n\nYou may need to convert `List\u003cint\u003e` to `Uint8List` to display images.\n\nTo use `Uint8List`, you need import package to your code like this:\n\n![img](https://raw.githubusercontent.com/CaiJingLong/asset_for_picgo/master/20190519111735.png)\n\n```dart\nfinal image = Uint8List.fromList(imageList);\nImageProvider provider = MemoryImage(Uint8List.fromList(imageList));\n```\n\nUsage in `Image` Widget:\n\n```dart\nFuture\u003cWidget\u003e _compressImage() async {\n  List\u003cint\u003e image = await testCompressFile(file);\n  ImageProvider provider = MemoryImage(Uint8List.fromList(image));\n  imageWidget = Image(\n    image: provider ?? AssetImage('img/img.jpg'),\n  );\n}\n```\n\nWrite to file usage:\n\n```dart\nFuture\u003cvoid\u003e writeToFile(List\u003cint\u003e image, String filePath) {\n  return File(filePath).writeAsBytes(image, flush: true);\n}\n```\n\n## Runtime Error\n\nBecause of some support issues,\nall APIs will be compatible with format and system compatibility,\nand an exception (`UnsupportedError`) may be thrown,\nso if you insist on using webp and heic formats,\nplease catch the exception yourself and use it on unsupported devices jpeg compression.\n\nExample:\n\n```dart\nFuture\u003cUint8List\u003e compressAndTryCatch(String path) async {\n  Uint8List result;\n  try {\n    result = await FlutterImageCompress.compressWithFile(\n      path,\n      format: CompressFormat.heic,\n    );\n  } on UnsupportedError catch (e) {\n    print(e);\n    result = await FlutterImageCompress.compressWithFile(\n      path,\n      format: CompressFormat.jpeg,\n    );\n  }\n  return result;\n}\n```\n\n## Android\n\nYou may need to update Kotlin to version `1.5.21` or higher.\n\n## Troubleshooting\n\n### Compressing returns `null`\n\nSometimes, compressing will return null. You should check if you can read/write the file,\nand the parent folder of the target file must exist.\n\nFor example, use the [path_provider](https://pub.dartlang.org/packages/path_provide)\nplugin to access some application folders,\nand use a permission plugin to request permission to access SD cards on Android/iOS.\n\n## About EXIF information\n\nUsing this library, EXIF information will be removed by default.\n\nEXIF information can be retained by setting keepExif to true,\nbut not `direction` information.\n\n- PNG/JPEG encoder: System API.\n- WebP encoder:\n  - [SDWebImageWebPCoder](https://github.com/SDWebImage/SDWebImageWebPCoder) on iOS.\n  - System API on Android.\n- HEIF encoder: System API.\n  - [HeifWriter](https://developer.android.com/jetpack/androidx/releases/heifwriter) on Android P+.\n\n## Web\n\nThe web implementation is not required for many people,\n\n## About macOS\n\nYou need change the minimum deployment target to 10.15.\n\nOpen xcode project, select Runner target, and change the value of `macOS Deployment Target` to `10.15`.\n\nAnd, change the `Podfile`:\nChange `platform` to `platform :osx, '10.15'`.\n\n## OpenHarmony\n\nThe currently supported image formats for parsing include JPEG, PNG, GIF, RAW, WebP, BMP, and SVG. However, the encoding output image formats are currently limited to JPEG, PNG, and WebP only.\n\n当前支持的解析图片格式包括 JPEG、PNG、GIF、RAW、WebP、BMP、SVG . 编码输出图片格式当前仅支持 JPEG、PNG 和 WebP.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluttercandies%2Fflutter_image_compress","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluttercandies%2Fflutter_image_compress","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluttercandies%2Fflutter_image_compress/lists"}