Memulai
-
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