compress_image
Reduce image file size without compromising quality by specifying input and output paths, compression quality, and progressive encoding for JPEGs.
Instructions
Compress an image with quality settings
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Path to input image | |
| outputPath | Yes | Path to save compressed image | |
| progressive | No | Use progressive encoding (for JPEG) | |
| quality | No | Compression quality (1-100) |
Implementation Reference
- src/index.ts:286-319 (handler)Handler function for compress_image tool. Compresses image using sharp library based on detected format and quality settings, computes size reduction, and returns success message.case 'compress_image': { const { inputPath, outputPath, quality = 80, progressive = true } = args; await fs.mkdir(path.dirname(outputPath), { recursive: true }); const metadata = await sharp(inputPath).metadata(); let pipeline = sharp(inputPath); // Apply compression based on format if (metadata.format === 'jpeg') { pipeline = pipeline.jpeg({ quality, progressive }); } else if (metadata.format === 'png') { pipeline = pipeline.png({ quality }); } else if (metadata.format === 'webp') { pipeline = pipeline.webp({ quality }); } else if (metadata.format === 'avif') { pipeline = pipeline.avif({ quality }); } await pipeline.toFile(outputPath); const originalSize = (await fs.stat(inputPath)).size; const compressedSize = (await fs.stat(outputPath)).size; const savings = ((originalSize - compressedSize) / originalSize * 100).toFixed(1); return { content: [ { type: 'text', text: `Image compressed successfully. Saved to: ${outputPath}\nSize reduction: ${savings}% (${originalSize} → ${compressedSize} bytes)` } ] }; }
- src/index.ts:94-116 (schema)Input schema definition for compress_image tool, including parameters for input/output paths, quality, and progressive encoding.name: 'compress_image', description: 'Compress an image with quality settings', inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: 'Path to input image' }, outputPath: { type: 'string', description: 'Path to save compressed image' }, quality: { type: 'number', minimum: 1, maximum: 100, description: 'Compression quality (1-100)', default: 80 }, progressive: { type: 'boolean', description: 'Use progressive encoding (for JPEG)', default: true } }, required: ['inputPath', 'outputPath'] } },
- src/index.ts:94-116 (registration)Registration of the compress_image tool in the ListTools response, including name, description, and schema.name: 'compress_image', description: 'Compress an image with quality settings', inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: 'Path to input image' }, outputPath: { type: 'string', description: 'Path to save compressed image' }, quality: { type: 'number', minimum: 1, maximum: 100, description: 'Compression quality (1-100)', default: 80 }, progressive: { type: 'boolean', description: 'Use progressive encoding (for JPEG)', default: true } }, required: ['inputPath', 'outputPath'] } },