Commencer
-
Installer the package
Fenêtre de terminal npm i @capgo/capacitor-file-compressorFenêtre de terminal pnpm add @capgo/capacitor-file-compressorFenêtre de terminal yarn add @capgo/capacitor-file-compressorFenêtre de terminal bun add @capgo/capacitor-file-compressor -
Synchroniser with Natif projects
Fenêtre de terminal npx cap syncFenêtre de terminal pnpm cap syncFenêtre de terminal yarn cap syncFenêtre de terminal bunx cap sync
Platform Support
Section titled “Platform Support”| Platform | Supported Formats | Notes |
|---|---|---|
| iOS | JPEG | Only JPEG compression supported |
| Android | JPEG, WebP | Both formats fully supported |
| Web | JPEG, WebP | Canvas API-based compression |
Remarque: EXIF metadata is removed during compression on all platforms.
Utilisation
Section titled “Utilisation”Importer the plugin and compress images:
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);};API Référence
Section titled “API Référence”compressImage(Options)
Section titled “compressImage(Options)”Compresses an image file with specified dimensions and quality Paramètres.
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);Important Notes:
- EXIF metadata is removed during compression on all platforms
- Aspect ratio is automatically maintained if only one dimension is provided
- Compressed files are saved to temporary directories on Natif platforms
getPluginVersion()
Section titled “getPluginVersion()”Get the Natif plugin Version.
const { version } = await FileCompressor.getPluginVersion();console.log('Plugin version:', version);Terminé Exemple
Section titled “Terminé Exemple”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; }}Bonnes pratiques
Section titled “Bonnes pratiques”- Quality Paramètres: Démarrer with 0.8 quality for a good balance between file size and image quality
- Resize Dimensions: Only specify dimensions when needed - aspect ratio is preserved automatically
- Format Selection: Use JPEG for photos and WebP for better compression (Android/Web only)
- Erreur Handling: Always wrap compression calls in try-catch blocks
- Cleanup: Remember to clean up temporary files after use
Dépannage
Section titled “Dépannage”Problèmes courants
Section titled “Problèmes courants”Image not compressing: Ensure the source path is valid and accessible Out of memory: Reduce target dimensions or compress images one at a time Format not supported: Check platform support table above