resize_image
Resize images to custom dimensions or named presets while preserving the original format and aspect ratio.
Instructions
Resize an image to custom dimensions or a named preset. Preserves the original format.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input_path | Yes | Absolute path to the source image file | |
| width | No | Target width in pixels | |
| height | No | Target height in pixels | |
| preset | No | Named size preset (overrides width/height) | |
| lock_aspect_ratio | No | Keep aspect ratio (default true) | |
| output_path | No | Where to save the output (defaults to same directory as input) |
Implementation Reference
- index.js:131-171 (handler)The "resize_image" tool is registered and implemented in index.js. It uses the 'sharp' library to resize images based on provided dimensions or presets.
server.tool( "resize_image", "Resize an image to custom dimensions or a named preset. Preserves the original format.", { input_path: z.string().describe("Absolute path to the source image file"), width: z.number().int().positive().optional().describe("Target width in pixels"), height: z.number().int().positive().optional().describe("Target height in pixels"), preset: z.enum([ "instagram-square", "instagram-portrait", "instagram-landscape", "twitter-post", "twitter-header", "full-hd", "4k", "youtube-thumbnail", "favicon", ]).optional().describe("Named size preset (overrides width/height)"), lock_aspect_ratio: z.boolean().optional().default(true).describe("Keep aspect ratio (default true)"), output_path: z.string().optional().describe("Where to save the output (defaults to same directory as input)"), }, async ({ input_path, width, height, preset, lock_aspect_ratio = true, output_path }) => { try { await fs.access(input_path); let targetW = width; let targetH = height; if (preset) { targetW = PRESETS[preset].width; targetH = PRESETS[preset].height; } if (!targetW && !targetH) { return { isError: true, content: [{ type: "text", text: "Provide width, height, or a preset." }] }; } const ext = path.extname(input_path).slice(1).toLowerCase() || "jpg"; const outPath = resolveOutputPath(input_path, ext, output_path); const fit = lock_aspect_ratio ? "inside" : "fill"; await sharp(input_path).resize(targetW, targetH, { fit }).toFile(outPath); const stat = await fs.stat(outPath); const meta = await sharp(outPath).metadata(); return { content: [{ type: "text", text: JSON.stringify({ success: true, output_path: outPath, width: meta.width, height: meta.height, size_bytes: stat.size }) }], }; } catch (err) { return { isError: true, content: [{ type: "text", text: `Error: ${err.message}` }] }; } } );