Skip to main content
Glama

Desktop Image Manager MCP Server

compress-image

Reduces image file size by compressing images with adjustable quality settings, enabling efficient storage and sharing of desktop images.

Instructions

压缩图片

Input Schema

NameRequiredDescriptionDefault
fileNameYes要压缩的图片文件名
outputNameNo输出文件名 (可选)
qualityNo压缩质量 (1-100)

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "fileName": { "description": "要压缩的图片文件名", "type": "string" }, "outputName": { "description": "输出文件名 (可选)", "type": "string" }, "quality": { "default": 80, "description": "压缩质量 (1-100)", "maximum": 100, "minimum": 1, "type": "number" } }, "required": [ "fileName" ], "type": "object" }

Implementation Reference

  • Executes the image compression logic: loads image from desktop using Sharp, applies quality-based compression depending on format (JPEG, PNG, WebP, or fallback to JPEG), saves to new file, computes and reports size savings in bytes and percentage, handles file existence, format validation, and errors.
    async ({ fileName, quality, outputName }) => { try { const desktopPath = getDesktopPath(); const inputPath = path.join(desktopPath, fileName); // 检查文件是否存在 if (!await fs.pathExists(inputPath)) { return { content: [{ type: "text", text: `文件 "${fileName}" 不存在。` }], isError: true }; } // 检查是否为图片文件 if (!isImageFile(inputPath)) { return { content: [{ type: "text", text: `文件 "${fileName}" 不是支持的图片格式。` }], isError: true }; } // 确定输出文件名 const ext = path.extname(fileName); const baseName = path.basename(fileName, ext); const finalOutputName = outputName || `${baseName}-compressed${ext}`; const outputNameFilled = isImageFile(finalOutputName) ? finalOutputName : `${finalOutputName}${ext}`; const outputPath = path.join(desktopPath, outputNameFilled); // 根据文件扩展名确定压缩方法 const lowerExt = ext.toLowerCase(); if (['.jpg', '.jpeg'].includes(lowerExt)) { await sharp(inputPath) .jpeg({ quality }) .toFile(outputPath); } else if (lowerExt === '.png') { await sharp(inputPath) .png({ quality }) .toFile(outputPath); } else if (lowerExt === '.webp') { await sharp(inputPath) .webp({ quality }) .toFile(outputPath); } else { // 对于其他格式,先转换为 JPEG 再压缩 await sharp(inputPath) .jpeg({ quality }) .toFile(outputPath); } // 获取原始文件和压缩后文件的大小 const originalSize = (await fs.stat(inputPath)).size; const compressedSize = (await fs.stat(outputPath)).size; const savingsPercent = ((originalSize - compressedSize) / originalSize * 100).toFixed(2); return { content: [{ type: "text", text: `图片压缩成功!\n原始文件: ${fileName} (${originalSize} 字节)\n压缩后文件: ${outputNameFilled} (${compressedSize} 字节)\n节省空间: ${savingsPercent}%` }] }; } catch (error) { return { content: [{ type: "text", text: `压缩图片时出错: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
  • Zod schema for 'compress-image' tool inputs: fileName (string, required), quality (number 1-100, default 80), outputName (optional string). Used for validation in the tool registration.
    { fileName: z.string().describe("要压缩的图片文件名"), quality: z.number().min(1).max(100).default(80).describe("压缩质量 (1-100)"), outputName: z.string().optional().describe("输出文件名 (可选)") },
  • server.ts:108-187 (registration)
    Registers the 'compress-image' tool on the MCP server with name, description, input schema, and handler function.
    server.tool( "compress-image", '压缩图片', { fileName: z.string().describe("要压缩的图片文件名"), quality: z.number().min(1).max(100).default(80).describe("压缩质量 (1-100)"), outputName: z.string().optional().describe("输出文件名 (可选)") }, async ({ fileName, quality, outputName }) => { try { const desktopPath = getDesktopPath(); const inputPath = path.join(desktopPath, fileName); // 检查文件是否存在 if (!await fs.pathExists(inputPath)) { return { content: [{ type: "text", text: `文件 "${fileName}" 不存在。` }], isError: true }; } // 检查是否为图片文件 if (!isImageFile(inputPath)) { return { content: [{ type: "text", text: `文件 "${fileName}" 不是支持的图片格式。` }], isError: true }; } // 确定输出文件名 const ext = path.extname(fileName); const baseName = path.basename(fileName, ext); const finalOutputName = outputName || `${baseName}-compressed${ext}`; const outputNameFilled = isImageFile(finalOutputName) ? finalOutputName : `${finalOutputName}${ext}`; const outputPath = path.join(desktopPath, outputNameFilled); // 根据文件扩展名确定压缩方法 const lowerExt = ext.toLowerCase(); if (['.jpg', '.jpeg'].includes(lowerExt)) { await sharp(inputPath) .jpeg({ quality }) .toFile(outputPath); } else if (lowerExt === '.png') { await sharp(inputPath) .png({ quality }) .toFile(outputPath); } else if (lowerExt === '.webp') { await sharp(inputPath) .webp({ quality }) .toFile(outputPath); } else { // 对于其他格式,先转换为 JPEG 再压缩 await sharp(inputPath) .jpeg({ quality }) .toFile(outputPath); } // 获取原始文件和压缩后文件的大小 const originalSize = (await fs.stat(inputPath)).size; const compressedSize = (await fs.stat(outputPath)).size; const savingsPercent = ((originalSize - compressedSize) / originalSize * 100).toFixed(2); return { content: [{ type: "text", text: `图片压缩成功!\n原始文件: ${fileName} (${originalSize} 字节)\n压缩后文件: ${outputNameFilled} (${compressedSize} 字节)\n节省空间: ${savingsPercent}%` }] }; } catch (error) { return { content: [{ type: "text", text: `压缩图片时出错: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );

Other Tools

Related Tools

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/zhixiaoqiang/desktop-image-manager-mcp'

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