Skip to content

aryapreetam/cmp-imgcompress

Repository files navigation

CMP Image Compressor

Compose Multiplatform Version Kotlin Version

Release workflow status Maven Central Version MIT License

Android iOS Desktop Web

A high-performance, lightweight image compression library for Compose Multiplatform (Android, iOS, Desktop/JVM, Web/WASM). It allows you to efficiently compress and resize images into high-quality WebP format using a simple, unified multiplatform API.

Key Features:

  • Dual Compression Modes: Compress by target quality percentage or target file size constraint (KB).
  • 📐 Smart Resizing: Fast, aspect-ratio-aware resizing prior to compression to save memory and network bandwidth.
  • ⚙️ Multi-Engine Execution: Uses high-performance native engines under the hood (Android's Bitmap, JVM's Skia, iOS's UIKit, Wasm's Canvas).
  • 📊 Rich Analytics: Delivers metadata and diagnostics on compression ratios, elapsed times, and iteration counts.
  • 🧪 Testable Design: Designed to support clean UI unit tests and portable multiplatform verification.

✅ Platform Capability Matrix

Capability Android iOS Desktop (JVM) Web (WASM)
WebP Compression
Compress By Quality
Compress By Target Size (Iterative) (Iterative) (Iterative)
Smart Auto-Resizing
Compression Analytics

Wasm Demo: Try Live Demo

API Docs: Library API Docs


📦 Installation

Add the dependency to your Compose Multiplatform project:

Option A — Version Catalog (Recommended)

  1. In gradle/libs.versions.toml:
[versions]
cmpImgcompress = "0.0.1"

[libraries]
cmp-imgcompress = { module = "io.github.aryapreetam:cmp-imgcompress", version.ref = "cmpImgcompress" }
  1. In your module's build.gradle.kts:
kotlin {
  sourceSets {
    val commonMain by getting {
      dependencies {
        implementation(libs.cmp.imgcompress)
      }
    }
  }
}

Option B — Direct Dependency

kotlin {
  sourceSets {
    val commonMain by getting {
      dependencies {
        implementation("io.github.aryapreetam:cmp-imgcompress:0.0.1")
      }
    }
  }
}

🚀 Quick Start

Basic Compression by Quality

import io.github.aryapreetam.cmpimgcompress.*

suspend fun compressMyImage(inputBytes: ByteArray) {
  val imageData = ImageData(inputBytes, "image/png")
  
  // Compress to WebP at 80% quality
  val compressed: CompressedImage = ImageCompressor.compress(
    input = imageData,
    config = CompressionConfig.ByQuality(80f)
  )
  
  println("Original: ${compressed.originalSize} bytes")
  println("Compressed: ${compressed.compressedSize} bytes")
  
  val outputWebpBytes = compressed.bytes
}

Compress to a Specific Target Size Constraint

import io.github.aryapreetam.cmpimgcompress.*

suspend fun compressToLimit(inputBytes: ByteArray) {
  val imageData = ImageData(inputBytes, "image/jpeg")
  
  // Attempt to compress the image to fall under 100 KB
  val compressed = ImageCompressor.compress(
    input = imageData,
    config = CompressionConfig.ByTargetSize(100), // Target size limit in KB
    resize = ResizeOptions(maxLongEdgePx = 1920) // Optional resizing
  )
  
  println("Elapsed time: ${compressed.metadata?.elapsedMillis} ms")
  println("Iterations taken: ${compressed.metadata?.iterations}")
}

📦 Download Sample App Executables

Download and try the sample app on your platform without building from source:

Platform Format / Package Download Link
🍏 macOS (Intel) DMG Installer Download
🍎 macOS (Apple Silicon) DMG Installer Download
🪟 Windows MSI Installer Download
🐧 Linux DEB Package Download
🤖 Android APK Executable Download
🌐 Web (Wasm) ZIP Bundle Download
iOS Simulator ZIP Bundle Download

🚀 How to Run the Sample App

For instructions on how to install and run the pre-built executables or build and run the sample application from source code, please refer to the How to Run the Sample App guide.


License

MIT License © 2026 aryapreetam and contributors. See LICENSE for details.

About

Image Compressor for Compose Multiplatform(only webp output for now)

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages