Skip to main content
Glama

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
NameRequiredDescriptionDefault
imagePathYesThe ABSOLUTE path to the image file to compress
outputFormatNoThe format to save the compressed image file
outputPathNoThe ABSOLUTE path to save the compressed image file
preserveMetadataNoThe metadata to preserve in the image file

Implementation Reference

  • 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: {}, }; },
  • 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];
  • 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', ];

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aiyogg/tinypng-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server