Skip to main content
Glama

resize_image

Resize images to specific dimensions and save them to new files using fit methods like cover, contain, or fill.

Instructions

Resize an image and save to a new file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputYesPath to the input image file
outputYesPath to save the resized image
widthYesTarget width in pixels
heightNoTarget height in pixels (optional)
fitNoFit method: cover, contain, fill, inside, outside

Implementation Reference

  • The handler function for the 'resize_image' tool within the CallToolRequestSchema handler. It extracts arguments, validates inputs, ensures output directory exists, resizes the image using Sharp library with specified width, optional height, and fit method, saves to output file, retrieves metadata, and returns a success message or error.
    case "resize_image": { const { input, output, width, height, fit = "cover" } = request.params.arguments as { input: string, output: string, width: number, height?: number, fit?: string }; if (!input || !output || !width) { throw new McpError(ErrorCode.InvalidParams, "Input path, output path, and width are required"); } if (!fs.existsSync(input)) { throw new McpError(ErrorCode.InvalidRequest, `Input image not found: ${input}`); } try { // Create output directory if it doesn't exist await fsExtra.ensureDir(path.dirname(output)); // Resize the image await sharp(input) .resize({ width, height, fit: fit as keyof sharp.FitEnum }) .toFile(output); // Get metadata of the resized image const metadata = await getImageMetadata(output); return { content: [ { type: "text", text: `Image resized successfully:\n` + `- Original: ${input}\n` + `- Resized: ${output}\n` + `- New dimensions: ${metadata.width}x${metadata.height}\n` + `- Size: ${(metadata.size / 1024).toFixed(2)} KB` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error resizing image: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
  • The input schema for the 'resize_image' tool, defined in the ListToolsRequestSchema response. Specifies required parameters (input path, output path, width) and optional (height, fit) with types and descriptions.
    name: "resize_image", description: "Resize an image and save to a new file", inputSchema: { type: "object", properties: { input: { type: "string", description: "Path to the input image file" }, output: { type: "string", description: "Path to save the resized image" }, width: { type: "number", description: "Target width in pixels" }, height: { type: "number", description: "Target height in pixels (optional)" }, fit: { type: "string", description: "Fit method: cover, contain, fill, inside, outside", enum: ["cover", "contain", "fill", "inside", "outside"] } }, required: ["input", "output", "width"] } },

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/rupeedev/mcp-image-reader'

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