Skip to content

Commit 13e1d44

Browse files
committed
feat: support watermark text shadow
1 parent 2f17203 commit 13e1d44

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

source/new-image-handler/src/processor/image/watermark.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class WatermarkAction extends BaseImageAction {
5555
}
5656

5757
public validate(params: string[]): ReadOnly<WatermarkOpts> {
58-
let opt: WatermarkOpts = {
58+
const opt: WatermarkOpts = {
5959
text: '',
6060
t: 100,
6161
g: 'southeast',
@@ -187,7 +187,7 @@ export class WatermarkAction extends BaseImageAction {
187187

188188
async textWaterMark(ctx: IImageContext, opt: WatermarkOpts): Promise<void> {
189189
const textOpt = this.calculateTextSize(opt.text, opt.size);
190-
const svg = this.textSvgStr(opt, textOpt);
190+
const svg = this.textSvgStr(opt, textOpt, true, opt.shadow / 100);
191191
const svgBytes = Buffer.from(svg);
192192
const metadata = await ctx.image.metadata();
193193
if (0 < opt.rotate) {
@@ -265,7 +265,7 @@ export class WatermarkAction extends BaseImageAction {
265265
async mixedWaterMark(ctx: IImageContext, opt: WatermarkOpts): Promise<void> {
266266
const bs = ctx.bufferStore;
267267
const textOpt = this.calculateTextSize(opt.text, opt.size);
268-
const svg = this.textSvgStr(opt, textOpt, false);
268+
const svg = this.textSvgStr(opt, textOpt, false, opt.shadow / 100);
269269
const svgBytes = Buffer.from(svg);
270270

271271
const watermarkImgBuffer = (await bs.get(opt.image)).buffer;
@@ -342,13 +342,16 @@ export class WatermarkAction extends BaseImageAction {
342342
height: Math.round(fontSize * 1.2),
343343
};
344344
}
345-
textSvgStr(opt: WatermarkOpts, textOpt: WatermarkTextOpts, applyOpacity: boolean = true): string {
345+
textSvgStr(opt: WatermarkOpts, textOpt: WatermarkTextOpts, applyOpacity: boolean = true, shadow: number = 0): string {
346346
const xOffset = Math.round(textOpt.width / 2);
347347
const yOffset = Math.round(textOpt.height * 0.8);
348348
const color = `#${opt.color}`;
349349
const opacity = applyOpacity ? opt.t / 100 : 1;
350+
// https://gitlab.gnome.org/GNOME/librsvg/-/blob/main/FEATURES.md
351+
// https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/529
352+
// https://github.com/lovell/sharp/issues/1490#issuecomment-1162760143
350353
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${textOpt.width} ${textOpt.height}" text-anchor="middle">
351-
<text font-size='${opt.size}' x="${xOffset}" y="${yOffset}" fill="${color}" opacity="${opacity}" font-family="${opt.type}">${opt.text}</text>
354+
<text filter="drop-shadow(rgba(0,0,0,${shadow}) 2px 0px 2px)" font-size="${opt.size}" x="${xOffset}" y="${yOffset}" fill="${color}" opacity="${opacity}" font-family="${opt.type}">${opt.text}</text>
352355
</svg>`;
353356
return svg;
354357
}

0 commit comments

Comments
 (0)