waterMarkFont
Add text watermarks to images stored in cloud storage. Specify the image file path and watermark text to create protected visual content.
Instructions
生成带文字水印的图片
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| objectKey | Yes | COS对象键(完整路径)示例: images/photo.jpg | |
| text | No | 水印文字内容(支持中文) | test |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"objectKey": {
"description": "COS对象键(完整路径)示例: images/photo.jpg",
"type": "string"
},
"text": {
"default": "test",
"description": "水印文字内容(支持中文)",
"type": "string"
}
},
"required": [
"objectKey"
],
"type": "object"
}
Implementation Reference
- src/services/ci/pic.service.ts:46-103 (handler)The handler function in CIPicService class that implements the watermark font logic using Tencent COS image processing API, including parameter validation, text encoding, and API call.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/services/ci/pic.service.ts:5-9 (schema)Zod schema definition for WaterMarkFont parameters used in the handler for input validation.export const WaterMarkFontParamsSchema = z.object({ objectKey: z.string(), text: z.string(), }); export type WaterMarkFontParams = z.infer<typeof WaterMarkFontParamsSchema>;
- src/server.ts:423-444 (registration)MCP server tool registration for 'waterMarkFont', defining input schema and delegating to CIPicInstance handler.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, }; }, );