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
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | Path to the input image file | |
| output | Yes | Path to save the resized image | |
| width | Yes | Target width in pixels | |
| height | No | Target height in pixels (optional) | |
| fit | No | Fit method: cover, contain, fill, inside, outside |
Implementation Reference
- src/index.ts:385-436 (handler)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 }; } }
- src/index.ts:252-281 (schema)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"] } },