batch_resize
Resize one image into multiple dimensions simultaneously. Specify custom widths, heights, and suffixes for each output. Save resized versions in JPEG, PNG, WebP, or AVIF formats efficiently.
Instructions
Generate multiple sizes from one image
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Output format for all sizes | |
| inputPath | Yes | Path to input image | |
| outputDir | Yes | Directory to save resized images | |
| sizes | Yes | Array of sizes to generate |
Implementation Reference
- src/index.ts:393-443 (handler)The batch_resize tool handler that processes the request by creating an output directory, extracting the base filename, iterating over the sizes array, resizing each image using Sharp with 'cover' fit and without enlargement, optionally converting to the specified format, saving each to the output directory with a suffix, and returning a success message with the list of generated files.case 'batch_resize': { const { inputPath, outputDir, sizes, format } = args; await fs.mkdir(outputDir, { recursive: true }); const basename = path.basename(inputPath, path.extname(inputPath)); const results = []; for (const size of sizes) { const outputName = `${basename}${size.suffix}${format ? `.${format}` : path.extname(inputPath)}`; const outputPath = path.join(outputDir, outputName); let pipeline = sharp(inputPath) .resize({ width: size.width, height: size.height || undefined, fit: 'cover', withoutEnlargement: true }); if (format) { switch (format) { case 'jpeg': case 'jpg': pipeline = pipeline.jpeg({ quality: 80, progressive: true }); break; case 'png': pipeline = pipeline.png(); break; case 'webp': pipeline = pipeline.webp({ quality: 80 }); break; case 'avif': pipeline = pipeline.avif({ quality: 80 }); break; } } await pipeline.toFile(outputPath); results.push(outputPath); } return { content: [ { type: 'text', text: `Batch resize complete. Generated ${results.length} images:\n${results.join('\n')}` } ] }; }
- src/index.ts:166-195 (schema)The tool registration block defining the name, description, and input schema (including properties for inputPath, outputDir, sizes array with width/suffix/optional height, and optional format) for the batch_resize tool.{ name: 'batch_resize', description: 'Generate multiple sizes from one image', inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: 'Path to input image' }, outputDir: { type: 'string', description: 'Directory to save resized images' }, sizes: { type: 'array', items: { type: 'object', properties: { width: { type: 'number', description: 'Width in pixels' }, height: { type: 'number', description: 'Height in pixels' }, suffix: { type: 'string', description: 'Suffix to add to filename' } }, required: ['width', 'suffix'] }, description: 'Array of sizes to generate' }, format: { type: 'string', enum: ['jpeg', 'jpg', 'png', 'webp', 'avif'], description: 'Output format for all sizes' } }, required: ['inputPath', 'outputDir', 'sizes'] } }