compress_image
Reduce image file size while maintaining quality using configurable compression settings for JPEG, PNG, WebP, and AVIF formats.
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 | |
| quality | No | Compression quality (1-100) | |
| progressive | No | Use progressive encoding (for JPEG) |
Implementation Reference
- src/index.ts:286-319 (handler)Handler for the 'compress_image' tool. Extracts arguments, creates output directory, detects image format using Sharp metadata, applies format-specific compression (JPEG, PNG, WebP, AVIF) with specified quality and progressive option, saves the compressed image, calculates and reports size reduction percentage.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:93-116 (registration)Registration of the 'compress_image' tool in the listTools handler, including name, description, and input schema for validation.{ 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:96-115 (schema)Input schema definition for the compress_image tool, specifying parameters like inputPath, outputPath, quality, and progressive.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'] }