Skip to main content
Glama

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
NameRequiredDescriptionDefault
input_pathYesAbsolute path to the source image file
widthNoTarget width in pixels
heightNoTarget height in pixels
presetNoNamed size preset (overrides width/height)
lock_aspect_ratioNoKeep aspect ratio (default true)
output_pathNoWhere to save the output (defaults to same directory as input)

Implementation Reference

  • 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}` }] };
        }
      }
    );

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/sonic0002/imagic-mcp'

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