@capgo/capacitor-file-compressor

中文文档
8.1.19发布于 2026/11/09MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 5.3万

Capacitor plugin for efficient image compression supporting PNG, JPEG, and WebP formats across iOS, Android, and Web platforms

原始使用文档
文档目录11 节
  1. ➡️ Get started for free
  2. Key features
  3. Why File Compressor?
  4. Documentation
  5. Compatibility
  6. Install
  7. Format Conversion
  8. API
  9. compressImage(...)
  10. getPluginVersion()
  11. Interfaces

@capgo/capacitor-file-compressor

Compress and resize images on the device before upload, and convert between JPEG, PNG and WebP. Smaller files mean faster uploads and lower storage costs.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-file-compressor in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-file-compressor to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-file-compressor for Capacitor apps

Key features

  • One call: compressImage() resizes and compresses with your width, height and quality.
  • Format conversion: output JPEG, PNG or WebP on Android and web. iOS adds HEIF or HEIC, and WebP needs iOS 14 or later.
  • Smart sizing: keeps the aspect ratio and never upscales.
  • Best-effort size: if the result is larger than the source, quality is lowered for lossy formats. PNG has no quality setting, so the output can still be larger.
  • Privacy: EXIF metadata is removed during compression.
  • Platforms: iOS, Android and Web. iOS uses ImageIO, Android uses Bitmap, web uses the canvas.

Why File Compressor?

A free, open-source alternative for client-side image compression:

  • Format conversion - Convert HEIF, PNG, WebP, GIF, and more to JPEG, PNG, WebP, or HEIF
  • Multiple formats - Broad input support with platform-specific output formats
  • Quality control - Adjustable compression quality (0.0 - 1.0)
  • Smart resizing - Automatic aspect ratio preservation without upscaling
  • Cross-platform - Consistent API across iOS, Android, and Web
  • Zero backend - All compression happens on the device

Essential for apps that need to optimize image uploads, reduce storage, or improve performance without server-side processing.

Documentation

The most complete doc is available here: https://capgo.app/docs/plugins/file-compressor/

Compatibility

Plugin versionCapacitor compatibilityMaintained
v8.*.*v8.*.*✅
v7.*.*v7.*.*On demand
v6.*.*v6.*.*❌
v5.*.*v5.*.*❌

Note: The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.

Install

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-file-compressor` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capgo/capacitor-file-compressor
npx cap sync

Format Conversion

The plugin accepts common image formats as input and converts them to the mimeType you request.

PlatformInput formatsOutput formats
iOSJPEG, PNG, WebP, GIF, BMP, TIFF, HEIF/HEICJPEG, PNG, HEIF/HEIC, WebP (iOS 14+)
AndroidJPEG, PNG, WebP, GIF, BMP, HEIF/HEIC (API 28+)JPEG, PNG, WebP
WebAny format the browser can decodeJPEG, PNG, WebP
import { FileCompressor } from '@capgo/capacitor-file-compressor';

// Convert an iPhone HEIF photo to JPEG before upload
const result = await FileCompressor.compressImage({
  path: heifFileUri,
  width: 1920,
  height: 1080,
  quality: 0.7,
  mimeType: 'image/jpeg',
});

Note: EXIF metadata is removed during compression on all platforms.

API

Capacitor File Compressor Plugin interface for image compression.

compressImage(...)

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

Compresses an image file with specified dimensions and quality settings.

This method compresses images to reduce file size while maintaining acceptable quality. It supports resizing and format conversion across common image formats.

Supported input formats (platform decoder dependent): JPEG, PNG, WebP, GIF, BMP, TIFF, HEIF/HEIC

Supported output formats:

  • iOS: image/jpeg, image/png, image/heif, image/heic, image/webp (iOS 14+)
  • Android: image/jpeg, image/png, image/webp
  • Web: image/jpeg, image/png, image/webp

Important Notes:

  • EXIF metadata is removed during compression on all platforms
  • Aspect ratio is automatically maintained if only one dimension is provided
  • When both width and height are provided, the image fits inside that box without upscaling
  • Compressed files are saved to temporary directories on native platforms
  • If the encoded output would be larger than the source file, quality is reduced automatically while keeping the requested output format
ParamTypeDescription
optionsCompressImageOptions- Configuration options for image compression

Returns: Promise<CompressImageResult>

Since: 7.0.0


getPluginVersion()

getPluginVersion() => Promise<{ version: string; }>

Get the native Capacitor plugin version.

Returns the version of the native plugin implementation. Useful for debugging and ensuring compatibility.

Returns: Promise<{ version: string; }>

Since: 7.0.0


Interfaces

CompressImageResult

The result of compressing an image.

Contains either a file path (native platforms) or a Blob (web platform) depending on where the compression was performed.

PropTypeDescriptionSince
pathstringThe 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
blobBlobThe 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 FileReader7.0.0

CompressImageOptions

Options for compressing an image.

Configure the compression behavior including quality, dimensions, and output format. Platform-specific options are available for path (native) and blob (web).

PropTypeDescriptionDefaultSince
pathstringThe 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 Common input formats include JPEG, PNG, WebP, GIF, BMP, TIFF, and HEIF/HEIC.7.0.0
blobBlobThe 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
qualitynumberThe 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. PNG output ignores quality because it is lossless.0.67.0.0
widthnumberThe 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 scaled to fit inside that box while preserving aspect ratio. Images are never upscaled.7.0.0
heightnumberThe 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 scaled to fit inside that box while preserving aspect ratio. Images are never upscaled.7.0.0
mimeTypestringThe MIME type of the compressed output image. Platform Support: - iOS: image/jpeg, image/png, image/heif, image/heic, image/webp (iOS 14+) - Android: image/jpeg, image/png, image/webp - Web: image/jpeg, image/png, image/webp Use this option to convert the source image to another format while compressing. Format Characteristics: - JPEG: Universal support, good for photos, no transparency - PNG: Lossless, supports transparency - WebP: Better compression, supports transparency - HEIF/HEIC: Efficient photo format on iOS"image/jpeg"7.0.0

版本与发布记录

@capgo/capacitor-file-compressor 共发布 54 个版本,首次发布于 2025/11/19;过去 12 个月发布了 54 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.19latest2026/11/09今天
8.1.182026/11/026 天前
8.1.172026/10/2613 天前
8.1.162026/10/1921 天前
8.1.152026/10/1525 天前
8.1.142026/10/1327 天前

关于 @capgo/capacitor-file-compressor 的常见问题

@capgo/capacitor-file-compressor 是什么?

Capacitor plugin for efficient image compression supporting PNG, JPEG, and WebP formats across iOS, Android, and Web platforms

如何安装 @capgo/capacitor-file-compressor?

在项目目录运行 npm install @capgo/capacitor-file-compressor,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capgo/capacitor-file-compressor 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capgo/capacitor-file-compressor 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capgo/capacitor-file-compressor 可以商用吗?

@capgo/capacitor-file-compressor 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-file-compressor 的下载量有多少?

根据 npm 官方下载统计,@capgo/capacitor-file-compressor 在 2026/10/09 – 2026/11/07 这 30 天内被下载 52,957 次,日均约 1,765 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capgo/capacitor-file-compressor 还在维护吗?

最新版本 8.1.19 发布于 2026/11/09(今天);至今共发布 54 个版本,其中过去 12 个月发布了 54 个;首次发布于 2025/11/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。