Skip to content

@capawesome-team/capacitor-file-compressor

Capacitor plugin for compressing files.

Features

  • 🖥️ Cross-platform: Supports Android, iOS and Web.
  • 🌅 Compress Images: Compress png, jpeg, and webp images.
  • 🔁 Up-to-date: Always supports the latest Capacitor version.
  • ⭐️ Support: First-class support from the Capawesome Team.

Installation

This plugin is only available to Capawesome Insiders. First, make sure you have the Capawesome npm registry set up. You can do this by running the following commands:

npm config set @capawesome-team:registry https://npm.registry.capawesome.io
npm config set //npm.registry.capawesome.io/:_authToken <YOUR_LICENSE_KEY>

Attention: Replace <YOUR_LICENSE_KEY> with the license key you received from Polar. If you don't have a license key yet, you can get one by becoming a Capawesome Insider.

Next, install the package:

npm install @capawesome-team/capacitor-file-compressor
npx cap sync

Android

Variables

This plugin will use the following project variables (defined in your app’s variables.gradle file):

  • $androidxDocumentFileVersion version of androidx.documentfile:documentfile (default: 1.0.1)

Configuration

No configuration required for this plugin.

Demo

A working example can be found here: capawesome-team/capacitor-plugin-demo

Android

Usage

import { FileCompressor } from '@capawesome-team/capacitor-file-compressor';

const compressImage = async () => {
  const { path } = await FileCompressor.compressImage({
    mimeType: 'image/jpeg',
    path: 'content://com.android.providers.downloads.documents/document/msf%3A1000000485',
    quality: 0.7,
  });
  return path;
};

API

compressImage(...)

compressImage(options: CompressImageOptions) => Promise<CompressImageResult>

Compress an image.

Only png, jpeg, and webp images are supported.

Attention: The exif data of the image is lost during compression.

Param Type
options CompressImageOptions

Returns: Promise<CompressImageResult>

Since: 5.0.0


Interfaces

CompressImageResult

Prop Type Description Since
path string The path of the compressed file. Only available on Android and iOS. 5.0.0
blob Blob The blob of the compressed file. Only available on Web. 5.0.0

CompressImageOptions

Prop Type Description Default Since
blob Blob The blob of the file to compress. Only available on Web. 5.0.0
mimeType string The mime type of the compressed file. On Android, only image/jpeg and image/webp are supported. On iOS, only image/jpeg is supported. On Web, only image/jpeg and image/webp are supported. 'image/jpeg' 5.0.0
path string The path of the file to compress. Only available on Android and iOS. 5.0.0
quality number The quality of the resulting image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality). 0.6 5.0.0

Changelog

See CHANGELOG.md.

License

See LICENSE.