@capgo/capacitor-file-compressor 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'CapgoCapacitorFileCompressor'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.license = package['license']
10
+ s.homepage = package['repository']['url']
11
+ s.author = package['author']
12
+ s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
13
+ s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
14
+ s.ios.deployment_target = '13.0'
15
+ s.dependency 'Capacitor'
16
+ s.swift_version = '5.1'
17
+ end
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Martin Donadieu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/Package.swift ADDED
@@ -0,0 +1,28 @@
1
+ // swift-tools-version: 5.9
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "CapgoCapacitorFileCompressor",
6
+ platforms: [.iOS(.v14)],
7
+ products: [
8
+ .library(
9
+ name: "CapgoCapacitorFileCompressor",
10
+ targets: ["FileCompressorPlugin"])
11
+ ],
12
+ dependencies: [
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.0.0")
14
+ ],
15
+ targets: [
16
+ .target(
17
+ name: "FileCompressorPlugin",
18
+ dependencies: [
19
+ .product(name: "Capacitor", package: "capacitor-swift-pm"),
20
+ .product(name: "Cordova", package: "capacitor-swift-pm")
21
+ ],
22
+ path: "ios/Sources/FileCompressorPlugin"),
23
+ .testTarget(
24
+ name: "FileCompressorPluginTests",
25
+ dependencies: ["FileCompressorPlugin"],
26
+ path: "ios/Tests/FileCompressorPluginTests")
27
+ ]
28
+ )
package/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # @capgo/capacitor-file-compressor
2
+ <a href="https://capgo.app/"><img src='https://raw.githubusercontent.com/Cap-go/capgo/main/assets/capgo_banner.png' alt='Capgo - Instant updates for capacitor'/></a>
3
+
4
+ <div align="center">
5
+ <h2><a href="https://capgo.app/?ref=plugin_file-compressor"> ➡️ Get Instant updates for your App with Capgo</a></h2>
6
+ <h2><a href="https://capgo.app/consulting/?ref=plugin_file-compressor"> Missing a feature? We'll build the plugin for you 💪</a></h2>
7
+ </div>
8
+
9
+ Capacitor plugin for efficient image compression supporting PNG, JPEG, and WebP formats across iOS, Android, and Web platforms.
10
+
11
+ ## Why File Compressor?
12
+
13
+ A free, open-source alternative for **client-side image compression**:
14
+
15
+ - **Multiple formats** - JPEG and WebP compression support
16
+ - **Quality control** - Adjustable compression quality (0.0 - 1.0)
17
+ - **Smart resizing** - Automatic aspect ratio preservation
18
+ - **Cross-platform** - Consistent API across iOS, Android, and Web
19
+ - **Zero backend** - All compression happens on the device
20
+
21
+ Essential for apps that need to optimize image uploads, reduce storage, or improve performance without server-side processing.
22
+
23
+ ## Documentation
24
+
25
+ The most complete doc is available here: https://capgo.app/docs/plugins/file-compressor/
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ npm install @capgo/capacitor-file-compressor
31
+ npx cap sync
32
+ ```
33
+
34
+ ## Platform Support
35
+
36
+ | Platform | Supported Formats | Notes |
37
+ |----------|-------------------|-------|
38
+ | iOS | JPEG | Only JPEG compression supported |
39
+ | Android | JPEG, WebP | Both formats fully supported |
40
+ | Web | JPEG, WebP | Canvas API-based compression |
41
+
42
+ Note: EXIF metadata is removed during compression on all platforms.
43
+
44
+ ## API
45
+
46
+ <docgen-index>
47
+
48
+ * [`compressImage(...)`](#compressimage)
49
+ * [`getPluginVersion()`](#getpluginversion)
50
+ * [Interfaces](#interfaces)
51
+
52
+ </docgen-index>
53
+
54
+ <docgen-api>
55
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
56
+
57
+ Capacitor File Compressor Plugin interface for image compression.
58
+
59
+ ### compressImage(...)
60
+
61
+ ```typescript
62
+ compressImage(options: CompressImageOptions) => Promise<CompressImageResult>
63
+ ```
64
+
65
+ Compresses an image file with specified dimensions and quality settings.
66
+
67
+ This method compresses images to reduce file size while maintaining acceptable quality.
68
+ It supports resizing and format conversion (JPEG/WebP depending on platform).
69
+
70
+ **Important Notes:**
71
+ - EXIF metadata is removed during compression on all platforms
72
+ - Aspect ratio is automatically maintained if only one dimension is provided
73
+ - Compressed files are saved to temporary directories on native platforms
74
+
75
+ | Param | Type | Description |
76
+ | ------------- | --------------------------------------------------------------------- | --------------------------------------------- |
77
+ | **`options`** | <code><a href="#compressimageoptions">CompressImageOptions</a></code> | - Configuration options for image compression |
78
+
79
+ **Returns:** <code>Promise&lt;<a href="#compressimageresult">CompressImageResult</a>&gt;</code>
80
+
81
+ **Since:** 7.0.0
82
+
83
+ --------------------
84
+
85
+
86
+ ### getPluginVersion()
87
+
88
+ ```typescript
89
+ getPluginVersion() => Promise<{ version: string; }>
90
+ ```
91
+
92
+ Get the native Capacitor plugin version.
93
+
94
+ Returns the version of the native plugin implementation.
95
+ Useful for debugging and ensuring compatibility.
96
+
97
+ **Returns:** <code>Promise&lt;{ version: string; }&gt;</code>
98
+
99
+ **Since:** 7.0.0
100
+
101
+ --------------------
102
+
103
+
104
+ ### Interfaces
105
+
106
+
107
+ #### CompressImageResult
108
+
109
+ The result of compressing an image.
110
+
111
+ Contains either a file path (native platforms) or a Blob (web platform)
112
+ depending on where the compression was performed.
113
+
114
+ | Prop | Type | Description | Since |
115
+ | ---------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
116
+ | **`path`** | <code>string</code> | The file path of the compressed image. **Platform:** Android, iOS only (undefined on Web) Points to a temporary file containing the compressed image. On iOS, typically in `NSTemporaryDirectory()`. On Android, typically in app cache directory. **Important:** These files may be cleaned up by the OS. Copy to permanent storage if needed for long-term use. | 7.0.0 |
117
+ | **`blob`** | <code>Blob</code> | The blob of the compressed image. **Platform:** Web only (undefined on iOS/Android) A Blob object containing the compressed image data. Can be used to: - Create object URLs for preview: `URL.createObjectURL(blob)` - Upload to server via FormData - Save to IndexedDB or other storage - Convert to base64 with FileReader | 7.0.0 |
118
+
119
+
120
+ #### CompressImageOptions
121
+
122
+ Options for compressing an image.
123
+
124
+ Configure the compression behavior including quality, dimensions, and output format.
125
+ Platform-specific options are available for path (native) and blob (web).
126
+
127
+ | Prop | Type | Description | Default | Since |
128
+ | -------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ----- |
129
+ | **`path`** | <code>string</code> | The file path of the image to compress. **Platform:** Android, iOS only (not supported on Web) Accepts various path formats: - iOS: `file://` URLs or absolute paths - Android: `content://` URIs, `file://` URLs, or absolute paths | | 7.0.0 |
130
+ | **`blob`** | <code>Blob</code> | The file blob of the image to compress. **Platform:** Web only (not supported on iOS/Android) Use this when compressing images from file inputs, fetch responses, or any other Blob source in web applications. | | 7.0.0 |
131
+ | **`quality`** | <code>number</code> | The quality of the compressed image. **Range:** 0.0 to 1.0 - `0.0` = Maximum compression (lowest quality, smallest file) - `1.0` = Minimum compression (highest quality, largest file) - `0.6` = Default balanced compression **Platform:** All platforms Higher quality values result in larger files but better visual quality. The actual compression ratio depends on the image content and format. | <code>0.6</code> | 7.0.0 |
132
+ | **`width`** | <code>number</code> | The width of the compressed image in pixels. **Platform:** All platforms If only width is specified, height is calculated automatically to maintain the original aspect ratio. If both width and height are specified, the image is resized to exact dimensions (may distort if ratio differs). | | 7.0.0 |
133
+ | **`height`** | <code>number</code> | The height of the compressed image in pixels. **Platform:** All platforms If only height is specified, width is calculated automatically to maintain the original aspect ratio. If both width and height are specified, the image is resized to exact dimensions (may distort if ratio differs). | | 7.0.0 |
134
+ | **`mimeType`** | <code>string</code> | The MIME type of the compressed output image. **Platform Support:** - **iOS:** `image/jpeg` only - **Android:** `image/jpeg`, `image/webp` - **Web:** `image/jpeg`, `image/webp` **Format Characteristics:** - **JPEG:** Universal support, good for photos, no transparency - **WebP:** Better compression, supports transparency, not on iOS | <code>"image/jpeg"</code> | 7.0.0 |
135
+
136
+ </docgen-api>
@@ -0,0 +1,57 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
6
+ }
7
+
8
+ buildscript {
9
+ repositories {
10
+ google()
11
+ mavenCentral()
12
+ }
13
+ dependencies {
14
+ classpath 'com.android.tools.build:gradle:8.7.2'
15
+ }
16
+ }
17
+
18
+ apply plugin: 'com.android.library'
19
+
20
+ android {
21
+ namespace "io.capgo.filecompressor"
22
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
23
+ defaultConfig {
24
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
25
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
26
+ versionCode 1
27
+ versionName "1.0"
28
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
29
+ }
30
+ buildTypes {
31
+ release {
32
+ minifyEnabled false
33
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34
+ }
35
+ }
36
+ lintOptions {
37
+ abortOnError false
38
+ }
39
+ compileOptions {
40
+ sourceCompatibility JavaVersion.VERSION_21
41
+ targetCompatibility JavaVersion.VERSION_21
42
+ }
43
+ }
44
+
45
+ repositories {
46
+ google()
47
+ mavenCentral()
48
+ }
49
+
50
+ dependencies {
51
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
52
+ implementation project(':capacitor-android')
53
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
54
+ testImplementation "junit:junit:$junitVersion"
55
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
56
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
57
+ }
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+ </manifest>
@@ -0,0 +1,163 @@
1
+ package io.capgo.filecompressor;
2
+
3
+ import android.graphics.Bitmap;
4
+ import android.graphics.BitmapFactory;
5
+ import android.net.Uri;
6
+ import android.webkit.MimeTypeMap;
7
+ import com.getcapacitor.JSObject;
8
+ import com.getcapacitor.Plugin;
9
+ import com.getcapacitor.PluginCall;
10
+ import com.getcapacitor.PluginMethod;
11
+ import com.getcapacitor.annotation.CapacitorPlugin;
12
+ import java.io.File;
13
+ import java.io.FileOutputStream;
14
+ import java.io.IOException;
15
+ import java.io.InputStream;
16
+ import java.util.UUID;
17
+
18
+ @CapacitorPlugin(name = "FileCompressor")
19
+ public class FileCompressorPlugin extends Plugin {
20
+
21
+ private final String pluginVersion = "7.0.0";
22
+
23
+ @PluginMethod
24
+ public void compressImage(PluginCall call) {
25
+ String path = call.getString("path");
26
+ if (path == null) {
27
+ call.reject("path is required");
28
+ return;
29
+ }
30
+
31
+ float quality = call.getFloat("quality", 0.6f);
32
+ Integer width = call.getInt("width");
33
+ Integer height = call.getInt("height");
34
+ String mimeType = call.getString("mimeType", "image/jpeg");
35
+
36
+ // Validate mime type for Android
37
+ if (!mimeType.equals("image/jpeg") && !mimeType.equals("image/webp")) {
38
+ call.reject("Only image/jpeg and image/webp are supported on Android");
39
+ return;
40
+ }
41
+
42
+ // Validate quality range
43
+ if (quality < 0.0f || quality > 1.0f) {
44
+ call.reject("quality must be between 0.0 and 1.0");
45
+ return;
46
+ }
47
+
48
+ try {
49
+ // Load bitmap from path
50
+ Bitmap bitmap = loadBitmapFromPath(path);
51
+ if (bitmap == null) {
52
+ call.reject("Failed to load image from path");
53
+ return;
54
+ }
55
+
56
+ // Resize bitmap if dimensions are provided
57
+ Bitmap processedBitmap = bitmap;
58
+ if (width != null && height != null) {
59
+ processedBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
60
+ } else if (width != null) {
61
+ float aspectRatio = (float) bitmap.getHeight() / bitmap.getWidth();
62
+ int targetHeight = (int) (width * aspectRatio);
63
+ processedBitmap = Bitmap.createScaledBitmap(bitmap, width, targetHeight, true);
64
+ } else if (height != null) {
65
+ float aspectRatio = (float) bitmap.getWidth() / bitmap.getHeight();
66
+ int targetWidth = (int) (height * aspectRatio);
67
+ processedBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, height, true);
68
+ }
69
+
70
+ // Compress bitmap
71
+ File compressedFile = compressBitmap(processedBitmap, quality, mimeType);
72
+ if (compressedFile == null) {
73
+ call.reject("Failed to compress image");
74
+ return;
75
+ }
76
+
77
+ // Clean up
78
+ if (processedBitmap != bitmap) {
79
+ processedBitmap.recycle();
80
+ }
81
+ bitmap.recycle();
82
+
83
+ // Return result
84
+ JSObject result = new JSObject();
85
+ result.put("path", compressedFile.getAbsolutePath());
86
+ call.resolve(result);
87
+ } catch (Exception e) {
88
+ call.reject("Error compressing image: " + e.getMessage(), e);
89
+ }
90
+ }
91
+
92
+ private Bitmap loadBitmapFromPath(String path) {
93
+ try {
94
+ // Handle content:// URIs
95
+ if (path.startsWith("content://")) {
96
+ Uri uri = Uri.parse(path);
97
+ InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
98
+ if (inputStream != null) {
99
+ Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
100
+ inputStream.close();
101
+ return bitmap;
102
+ }
103
+ }
104
+ // Handle file:// URIs and absolute paths
105
+ else if (path.startsWith("file://")) {
106
+ path = path.substring(7); // Remove "file://" prefix
107
+ }
108
+
109
+ // Try loading as file path
110
+ File file = new File(path);
111
+ if (file.exists()) {
112
+ return BitmapFactory.decodeFile(file.getAbsolutePath());
113
+ }
114
+ } catch (Exception e) {
115
+ e.printStackTrace();
116
+ }
117
+ return null;
118
+ }
119
+
120
+ private File compressBitmap(Bitmap bitmap, float quality, String mimeType) {
121
+ try {
122
+ // Determine compression format and file extension
123
+ Bitmap.CompressFormat format;
124
+ String extension;
125
+
126
+ if (mimeType.equals("image/webp")) {
127
+ format = Bitmap.CompressFormat.WEBP;
128
+ extension = ".webp";
129
+ } else {
130
+ format = Bitmap.CompressFormat.JPEG;
131
+ extension = ".jpg";
132
+ }
133
+
134
+ // Create temporary file
135
+ File tempDir = getContext().getCacheDir();
136
+ String fileName = "compressed_" + UUID.randomUUID().toString() + extension;
137
+ File compressedFile = new File(tempDir, fileName);
138
+
139
+ // Compress and save
140
+ FileOutputStream outputStream = new FileOutputStream(compressedFile);
141
+ int qualityInt = (int) (quality * 100);
142
+ bitmap.compress(format, qualityInt, outputStream);
143
+ outputStream.flush();
144
+ outputStream.close();
145
+
146
+ return compressedFile;
147
+ } catch (IOException e) {
148
+ e.printStackTrace();
149
+ return null;
150
+ }
151
+ }
152
+
153
+ @PluginMethod
154
+ public void getPluginVersion(final PluginCall call) {
155
+ try {
156
+ final JSObject ret = new JSObject();
157
+ ret.put("version", this.pluginVersion);
158
+ call.resolve(ret);
159
+ } catch (final Exception e) {
160
+ call.reject("Could not get plugin version", e);
161
+ }
162
+ }
163
+ }