waterMarkFont
Add custom text watermarks to images stored in Tencent Cloud COS, enabling branding or copyright protection without manual editing.
Instructions
生成带文字水印的图片
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| objectKey | Yes | COS对象键(完整路径)示例: images/photo.jpg | |
| text | No | 水印文字内容(支持中文) | test |
Implementation Reference
- src/services/ci/pic.service.ts:46-103 (handler)The core handler function `waterMarkFont` in `CIPicService` that applies a text-based watermark to an image using Tencent Cloud COS image processing API. It validates params, encodes text, constructs image process params, and sends POST request to COS.async waterMarkFont(params: WaterMarkFontParams) { // 验证并解析参数 const validParams = WaterMarkFontParamsSchema.parse(params); const { objectKey, text } = validParams; const encodedText = Buffer.from(text) .toString('base64') .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=+$/, ''); try { const imageProcessParams = [ 'watermark/2', // 水印类型2表示文字水印 `text/${encodedText}`, // 使用动态文本 'scatype/3', 'spcent/20', ].join('/'); const outPutFileid = generateOutPutFileId(objectKey); const result = await new Promise((resolve, reject) => { this.cos.request( { Bucket: this.bucket, // 存储桶,必须字段 Region: this.region, // 存储桶所在地域,必须字段 如 ap-beijing Key: objectKey, // 对象文件名,例如:folder/document.jpg。 Method: 'POST', // 固定值 Action: 'image_process', // 固定值 Headers: { 'Pic-Operations': JSON.stringify({ rules: [{ fileid: outPutFileid, rule: imageProcessParams }], }), }, }, function (error, data) { if (error) { // 处理请求失败 reject(error); } else { // 处理请求成功 resolve(data); } }, ); }); return { isSuccess: true, message: '添加水印成功', data: result, }; } catch (error) { return { isSuccess: false, message: '添加水印失败', data: error, }; } }
- src/server.ts:423-444 (registration)Registration of the MCP tool 'waterMarkFont' using server.tool(), providing description, Zod input schema for objectKey and text, and async handler that delegates to CIPicInstance.waterMarkFont.server.tool( 'waterMarkFont', '生成带文字水印的图片', { objectKey: z .string() .describe('COS对象键(完整路径)示例: images/photo.jpg'), text: z.string().describe('水印文字内容(支持中文)').default('test'), }, async ({ objectKey, text }) => { const res = await CIPicInstance.waterMarkFont({ objectKey, text }); return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: !res.isSuccess, }; }, );
- src/services/ci/pic.service.ts:5-9 (schema)Zod schema definition `WaterMarkFontParamsSchema` and inferred TypeScript type `WaterMarkFontParams` for input validation within the handler.export const WaterMarkFontParamsSchema = z.object({ objectKey: z.string(), text: z.string(), }); export type WaterMarkFontParams = z.infer<typeof WaterMarkFontParamsSchema>;