Export Image
photopea_export_imageExports the active document from Photopea to a local file. Supports PNG, JPG, WebP, PSD, and SVG formats with optional JPEG quality control.
Instructions
Export the active document to a file and save it to the local filesystem. The entire document is flattened and exported in the chosen format. Use create_document or open_file to set up the document before exporting.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | Yes | Output format: 'png' for lossless, 'jpg' for compressed photos, 'webp' for web, 'psd' for Photoshop, 'svg' for vector | |
| quality | No | Compression quality for JPG format only (1 = smallest file, 100 = best quality). Ignored for other formats. | |
| outputPath | Yes | Absolute local file path where the exported file will be saved (e.g. /Users/me/output.png) |
Implementation Reference
- src/tools/export.ts:14-39 (handler)The registerExportTools function registers the 'photopea_export_image' tool on the MCP server. The handler (lines 25-39) builds an export script via buildExportImage, sends it to the Photopea bridge, and writes the resulting file data to the local filesystem using writeLocalFile.
export function registerExportTools(server: McpServer, bridge: PhotopeaBridge): void { // 30. photopea_export_image server.registerTool("photopea_export_image", { title: "Export Image", description: "Export the active document to a file and save it to the local filesystem. The entire document is flattened and exported in the chosen format. Use create_document or open_file to set up the document before exporting.", inputSchema: { format: z.enum(["png", "jpg", "webp", "psd", "svg"]).describe("Output format: 'png' for lossless, 'jpg' for compressed photos, 'webp' for web, 'psd' for Photoshop, 'svg' for vector"), quality: z.number().min(1).max(100).optional().describe("Compression quality for JPG format only (1 = smallest file, 100 = best quality). Ignored for other formats."), outputPath: z.string().describe("Absolute local file path where the exported file will be saved (e.g. /Users/me/output.png)"), }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }, }, async (params) => { const script = buildExportImage(params); bridge.sendActivity({ type: "activity", id: "", tool: "export_image", summary: `Export as ${params.format} to ${params.outputPath}` }); const rawResult = await bridge.executeScript(script, true); if (!rawResult.success) return { isError: true, content: [{ type: "text" as const, text: rawResult.error || "Failed to export image" }] }; const fileResult = rawResult as BridgeFileResult; try { await writeLocalFile(params.outputPath, fileResult.data); } catch (err) { return { isError: true, content: [{ type: "text" as const, text: `Export succeeded but failed to write file: ${(err as Error).message}` }] }; } return { content: [{ type: "text" as const, text: `Image exported to: ${params.outputPath}` }] }; }); - src/tools/export.ts:16-25 (registration)Registration of the tool 'photopea_export_image' on the MCP server via server.registerTool with title, description, inputSchema, and annotations.
server.registerTool("photopea_export_image", { title: "Export Image", description: "Export the active document to a file and save it to the local filesystem. The entire document is flattened and exported in the chosen format. Use create_document or open_file to set up the document before exporting.", inputSchema: { format: z.enum(["png", "jpg", "webp", "psd", "svg"]).describe("Output format: 'png' for lossless, 'jpg' for compressed photos, 'webp' for web, 'psd' for Photoshop, 'svg' for vector"), quality: z.number().min(1).max(100).optional().describe("Compression quality for JPG format only (1 = smallest file, 100 = best quality). Ignored for other formats."), outputPath: z.string().describe("Absolute local file path where the exported file will be saved (e.g. /Users/me/output.png)"), }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }, }, async (params) => { - src/bridge/types.ts:243-247 (schema)Type definition for ExportImageParams used by the tool handler and script builder.
export interface ExportImageParams { format: "png" | "jpg" | "webp" | "psd" | "svg"; quality?: number; outputPath: string; } - src/bridge/script-builder.ts:804-817 (helper)buildExportImage generates the Photopea ExtendScript that calls app.activeDocument.saveToOE() to export the active document in the specified format.
export function buildExportImage(params: ExportImageParams): string { const { format, quality } = params; const lines: string[] = []; let formatStr: string; if (format === "jpg" && quality !== undefined) { formatStr = `jpg:${quality}`; } else { formatStr = format; } lines.push(`app.activeDocument.saveToOE('${formatStr}');`); return lines.join("\n"); } - src/utils/file-io.ts:12-18 (helper)writeLocalFile writes the exported Buffer data to the specified file path, creating directories if needed.
export async function writeLocalFile(filePath: string, data: Buffer): Promise<void> { const dir = dirname(filePath); if (!existsSync(dir)) { await mkdir(dir, { recursive: true }); } await writeFile(filePath, data); }