快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-file-compressor`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/file-compressor/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-file-compressor` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-file-compressorbunx cap sync导入
“导入”章节import { FileCompressor } from '@capgo/capacitor-file-compressor';快速入门
章节标题为“快速入门”import { FileCompressor } from '@capgo/capacitor-file-compressor';
const result = await FileCompressor.compressImage({ path: photoUri, width: 1280, quality: 0.8, mimeType: 'image/jpeg',});
await uploadFile(result.path);示例:Web 端 Blob 输入
标题为“示例:Web 端 Blob 输入”的章节const result = await FileCompressor.compressImage({ blob: pickedFile, quality: 0.75, width: 1024, mimeType: 'image/webp',});常见坑点
章节标题为“常见陷阱”- 不支持放大:仅会缩小大图尺寸。
- PNG 体积:PNG 没有质量调节选项,输出体积可能超过原图。
- EXIF:元数据会被剥离,压缩后请勿依赖方向标签。
API 概览
章节标题:“API 概览”compressImage
标题为 “compressImage” 的章节按指定的尺寸和质量设置压缩图像文件。
该方法通过压缩图像来减小文件体积,同时保持可接受的质量。 支持调整尺寸和格式转换(根据平台支持 JPEG/WebP)。
注意事项:
- 在所有平台上,压缩过程中都会移除 EXIF 元数据
- 如果仅提供一个维度,则会自动保持宽高比
- 压缩后的文件会保存在原生平台的临时目录中
import { FileCompressor } from '@capgo/capacitor-file-compressor';
// Web - Compress from file inputconst fileInput = document.getElementById('file') as HTMLInputElement;const file = fileInput.files[0];const result = await FileCompressor.compressImage({ blob: file, quality: 0.8, width: 1200, mimeType: 'image/jpeg'});const url = URL.createObjectURL(result.blob);类型参考
“类型参考” 章节CompressImageOptions
“CompressImageOptions”章节图像压缩选项。
export interface CompressImageOptions { /** * 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 * * @since 7.0.0 * @example "file:///var/mobile/Containers/Data/Application/image.jpg" // iOS * @example "content://com.android.providers.downloads.documents/document/msf%3A1000000485" // Android * @example "/storage/emulated/0/Download/photo.png" // Android absolute path */ path?: string;
/** * 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. * * @since 7.0.0 * @example * ```typescript * // From file input * const fileInput = document.getElementById('file') as HTMLInputElement; * const blob = fileInput.files[0]; * ``` * @example * ```typescript * // From fetch * const response = await fetch('https://example.com/image.jpg'); * const blob = await response.blob(); * ``` */ blob?: Blob;
/** * 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. * * @since 7.0.0 * @default 0.6 * @example 0.8 // High quality * @example 0.5 // Medium quality, smaller file * @example 0.3 // Low quality, very small file */ quality?: number;
/** * 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). * * @since 7.0.0 * @example 1920 // Full HD width * @example 1200 // Common web image width * @example 800 // Mobile-optimized width */ width?: number;
/** * 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). * * @since 7.0.0 * @example 1080 // Full HD height * @example 800 // Common web image height * @example 600 // Mobile-optimized height */ height?: number;
/** * 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 * * @since 7.0.0 * @default "image/jpeg" * @example "image/jpeg" // JPEG format (all platforms) * @example "image/.webp" // WebP format (Android/Web only) */ mimeType?: string;}CompressImageResult
标题为 “CompressImageResult” 的章节压缩图像后的结果。
export interface CompressImageResult { /** * 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. * * @since 7.0.0 * @example "/var/mobile/Containers/Data/tmp/compressed_abc123.jpg" // iOS * @example "/data/user/0/com.app/cache/compressed_xyz789.jpg" // Android */ path?: string;
/** * 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 * * @since 7.0.0 * @example * ```typescript * // Create preview URL * const url = URL.createObjectURL(result.blob); * imageElement.src = url; * ``` * @example * ```typescript * // Upload to server * const formData = new FormData(); * formData.append('image', result.blob, 'compressed.jpg'); * await fetch('/upload', { method: 'POST', body: formData }); * ``` */ blob?: Blob;}单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划存储和文件处理,请将其与 使用 @capgo/capacitor-file-compressor 指代使用 @capgo/capacitor-file-compressor 中的原生能力, @capgo/capacitor-data-storage-sqlite 关于 @capgo/capacitor-data-storage-sqlite 中的实现细节, 使用 @capgo/capacitor-data-storage-sqlite 关于使用 @capgo/capacitor-data-storage-sqlite 的原生能力, @capgo/capacitor-file 指代 @capgo/capacitor-file 中的实现细节,以及 使用 @capgo/capacitor-file 指代使用 @capgo/capacitor-file. 中的原生能力