compress_local_image
Reduce image file size by compressing local images while maintaining quality. Specify input and output paths, choose format, and optionally preserve metadata.
Instructions
Compress a local image file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imagePath | Yes | The ABSOLUTE path to the image file to compress | |
| outputFormat | No | The format to save the compressed image file | |
| outputPath | No | The ABSOLUTE path to save the compressed image file | |
| preserveMetadata | No | The metadata to preserve in the image file |
Implementation Reference
- src/tools.ts:119-171 (handler)Core handler function that performs local image compression using Tinify API: checks file size, sets API key, loads source, optionally preserves metadata or converts format, determines output path (appends _compressed if needed), saves file, computes and returns original size, compressed size, and compression ratio.async function handleCompressLocalImageTool({ imagePath, outputPath, outputFormat, preserveMetadata, }: { imagePath: string; outputPath?: string; outputFormat?: SupportedImageTypes; preserveMetadata?: string[]; }) { const originalSize = fs.statSync(imagePath).size; tinify.key = config.apiKey!; let source = tinify.fromFile(imagePath); if (preserveMetadata?.length) { source = source.preserve(...preserveMetadata); } let ext = path.extname(imagePath).slice(1); if (outputFormat) { source.convert({ type: outputFormat, }); ext = outputFormat.split('/')[1]; } let dest = outputPath; if (!dest) { const dir = path.dirname(imagePath); const basename = path.basename(imagePath, path.extname(imagePath)); dest = path.join(dir, `${basename}.${ext}`); } // add _compressed to the filename const destDir = path.dirname(dest); const destBasename = path.basename(dest, path.extname(dest)); if (!destBasename.endsWith('_compressed')) { dest = path.join(destDir, `${destBasename}_compressed.${ext}`); } await source.toFile(dest); const compressedSize = fs.statSync(dest).size; const compressionRatio = (originalSize - compressedSize) / originalSize; return { originalSize, compressedSize, compressionRatio, }; }
- src/tools.ts:244-261 (registration)Registers the 'compress_local_image' tool handler in TOOL_HANDLERS object: extracts arguments, calls the core handleCompressLocalImageTool function, formats response as MCP content with JSON-stringified result.compress_local_image: async (request) => { const result = await handleCompressLocalImageTool( request.params.arguments as { imagePath: string; outputPath?: string; outputFormat?: SupportedImageTypes; }, ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], metadata: {}, }; },
- src/tools.ts:17-50 (schema)Tool schema definition for 'compress_local_image': includes name, description, and detailed inputSchema specifying required imagePath, optional outputPath/outputFormat/preserveMetadata with enums and examples.const COMPRESS_LOCAL_IMAGE_TOOL: Tool = { name: 'compress_local_image', description: 'Compress a local image file', inputSchema: { type: 'object', properties: { imagePath: { type: 'string', description: 'The ABSOLUTE path to the image file to compress', example: '/Users/user/Downloads/image.jpg', }, outputPath: { type: 'string', description: 'The ABSOLUTE path to save the compressed image file', example: '/Users/user/Downloads/image_compressed.jpg', }, outputFormat: { type: 'string', description: 'The format to save the compressed image file', enum: SUPPORTED_IMAGE_TYPES, example: 'image/jpeg', }, preserveMetadata: { type: 'array', description: 'The metadata to preserve in the image file', items: { type: 'string', enum: ['copyright', 'creation', 'location'], }, }, }, required: ['imagePath'], }, };
- src/tools.ts:117-117 (registration)Registers COMPRESS_LOCAL_IMAGE_TOOL in the exported TOOLS array for tool discovery and schema provision.export const TOOLS = [COMPRESS_LOCAL_IMAGE_TOOL, COMPRESS_REMOTE_IMAGE_TOOL, RESIZE_IMAGE_TOOL];
- src/tools.ts:9-15 (helper)Defines supported image types used in the tool's outputFormat enum schema.const SUPPORTED_IMAGE_TYPES: SupportedImageTypes[] = [ 'image/jpeg', 'image/png', 'image/webp', 'image/jpg', 'image/avif', ];