Memulai
Copy a setup prompt with the install steps and the full markdown guide for this plugin.
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/id/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.
-
Instal paket
Terminal window npm i @capgo/capacitor-file-compressorTerminal window pnpm add @capgo/capacitor-file-compressorTerminal window yarn add @capgo/capacitor-file-compressorTerminal window bun add @capgo/capacitor-file-compressor -
Sinkronkan dengan proyek native
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
Dukungan Platform
Section titled “Dukungan Platform”| Platform | Format yang Didukung | Catatan |
|---|---|---|
| iOS | JPEG | Hanya kompresi JPEG yang didukung |
| Android | JPEG, WebP | Kedua format sepenuhnya didukung |
| Web | JPEG, WebP | Kompresi berbasis Canvas API |
Catatan: Metadata EXIF dihapus selama kompresi pada semua platform.
Penggunaan
Section titled “Penggunaan”Import plugin dan kompres gambar:
import { FileCompressor } from '@capgo/capacitor-file-compressor';
// Compress an imageconst compressImage = async () => { const result = await FileCompressor.compressImage({ source: 'file:///path/to/image.jpg', quality: 0.8, width: 1920, height: 1080 });
console.log('Compressed image path:', result.path); console.log('Original size:', result.originalSize); console.log('Compressed size:', result.size);};Referensi API
Section titled “Referensi API”compressImage(options)
Section titled “compressImage(options)”Mengompres file gambar dengan dimensi dan pengaturan kualitas yang ditentukan.
interface CompressImageOptions { source: string; // Path to the image file quality?: number; // 0.0 to 1.0 (default: 0.8) width?: number; // Target width in pixels height?: number; // Target height in pixels format?: 'jpeg' | '.webp'; // Output format}
interface CompressImageResult { path: string; // Path to compressed image size: number; // Compressed file size in bytes originalSize: number; // Original file size in bytes}
const result = await FileCompressor.compressImage(options);Catatan Penting:
- Metadata EXIF dihapus selama kompresi pada semua platform
- Aspect ratio secara otomatis dipertahankan jika hanya satu dimensi yang disediakan
- File terkompresi disimpan ke direktori sementara pada platform native
getPluginVersion()
Section titled “getPluginVersion()”Dapatkan versi plugin native.
const { version } = await FileCompressor.getPluginVersion();console.log('Plugin version:', version);Contoh Lengkap
Section titled “Contoh Lengkap”import { FileCompressor } from '@capgo/capacitor-file-compressor';import { Camera } from '@capacitor/camera';
export class ImageCompressionService { async captureAndCompress() { try { // Take a photo const photo = await Camera.getPhoto({ quality: 100, allowEditing: false, resultType: 'uri' });
if (!photo.path) { throw new Error('No image path'); }
// Compress the photo const compressed = await FileCompressor.compressImage({ source: photo.path, quality: 0.7, width: 1920, height: 1080, format: 'jpeg' });
console.log(`Compression ratio: ${ ((1 - compressed.size / compressed.originalSize) * 100).toFixed(1) }%`);
return compressed.path; } catch (error) { console.error('Compression failed:', error); throw error; } }
async batchCompress(imagePaths: string[]) { const results = [];
for (const path of imagePaths) { try { const result = await FileCompressor.compressImage({ source: path, quality: 0.8, width: 1280 }); results.push(result); } catch (error) { console.error(`Failed to compress ${path}:`, error); } }
return results; }}Praktik Terbaik
Section titled “Praktik Terbaik”- Pengaturan Kualitas: Mulai dengan kualitas 0.8 untuk keseimbangan yang baik antara ukuran file dan kualitas gambar
- Dimensi Resize: Hanya tentukan dimensi ketika diperlukan - aspect ratio dipertahankan secara otomatis
- Pemilihan Format: Gunakan JPEG untuk foto dan WebP untuk kompresi lebih baik (hanya Android/Web)
- Penanganan Error: Selalu bungkus panggilan kompresi dalam blok try-catch
- Cleanup: Ingat untuk membersihkan file sementara setelah digunakan
Pemecahan Masalah
Section titled “Pemecahan Masalah”Masalah Umum
Section titled “Masalah Umum”Gambar tidak dikompres: Pastikan path sumber valid dan dapat diakses Out of memory: Kurangi dimensi target atau kompres gambar satu per satu Format tidak didukung: Periksa tabel dukungan platform di atas