resize_image
Adjust image dimensions precisely by specifying width, height, and fit options. Maintain aspect ratio or customize fit for desired output. Save resized images in supported formats.
Instructions
Resize an image to specified dimensions
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fit | No | How the image should be resized to fit | cover |
| height | No | Target height in pixels | |
| inputPath | Yes | Path to input image | |
| outputPath | Yes | Path to save resized image | |
| preserveAspectRatio | No | Maintain original aspect ratio | |
| width | No | Target width in pixels |
Input Schema (JSON Schema)
{
"properties": {
"fit": {
"default": "cover",
"description": "How the image should be resized to fit",
"enum": [
"cover",
"contain",
"fill",
"inside",
"outside"
],
"type": "string"
},
"height": {
"description": "Target height in pixels",
"type": "number"
},
"inputPath": {
"description": "Path to input image",
"type": "string"
},
"outputPath": {
"description": "Path to save resized image",
"type": "string"
},
"preserveAspectRatio": {
"default": true,
"description": "Maintain original aspect ratio",
"type": "boolean"
},
"width": {
"description": "Target width in pixels",
"type": "number"
}
},
"required": [
"inputPath",
"outputPath"
],
"type": "object"
}
Implementation Reference
- src/index.ts:204-230 (handler)The main execution logic for the resize_image tool, using Sharp to create a resize pipeline and save the output image.case 'resize_image': { const { inputPath, outputPath, width, height, fit = 'cover', preserveAspectRatio = true } = args; await fs.mkdir(path.dirname(outputPath), { recursive: true }); let pipeline = sharp(inputPath); if (width || height) { pipeline = pipeline.resize({ width: width || undefined, height: height || undefined, fit: fit as any, withoutEnlargement: preserveAspectRatio }); } await pipeline.toFile(outputPath); return { content: [ { type: 'text', text: `Image resized successfully. Saved to: ${outputPath}` } ] }; }
- src/index.ts:31-51 (schema)Input schema defining the parameters for the resize_image tool, including paths, dimensions, fit mode, and aspect ratio preservation.inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: 'Path to input image' }, outputPath: { type: 'string', description: 'Path to save resized image' }, width: { type: 'number', description: 'Target width in pixels' }, height: { type: 'number', description: 'Target height in pixels' }, fit: { type: 'string', enum: ['cover', 'contain', 'fill', 'inside', 'outside'], description: 'How the image should be resized to fit', default: 'cover' }, preserveAspectRatio: { type: 'boolean', description: 'Maintain original aspect ratio', default: true } }, required: ['inputPath', 'outputPath'] }
- src/index.ts:28-52 (registration)Registration of the resize_image tool within the ListToolsRequestSchema response, including name, description, and input schema.{ name: 'resize_image', description: 'Resize an image to specified dimensions', inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: 'Path to input image' }, outputPath: { type: 'string', description: 'Path to save resized image' }, width: { type: 'number', description: 'Target width in pixels' }, height: { type: 'number', description: 'Target height in pixels' }, fit: { type: 'string', enum: ['cover', 'contain', 'fill', 'inside', 'outside'], description: 'How the image should be resized to fit', default: 'cover' }, preserveAspectRatio: { type: 'boolean', description: 'Maintain original aspect ratio', default: true } }, required: ['inputPath', 'outputPath'] } },