1
+ import { encode } from 'html-entities' ;
1
2
import * as sharp from 'sharp' ;
2
3
import { IImageContext } from '.' ;
3
4
import { IActionOpts , ReadOnly , InvalidArgument , Features , IProcessContext } from '..' ;
4
5
import * as is from '../../is' ;
5
6
import { BaseImageAction , split1 } from './_base' ;
6
7
8
+
7
9
export interface WatermarkOpts extends IActionOpts {
8
10
text : string ;
9
11
t : number ; // 不透明度
@@ -72,12 +74,12 @@ export class WatermarkAction extends BaseImageAction {
72
74
const [ k , v ] = split1 ( param , '_' ) ;
73
75
if ( k === 'text' ) {
74
76
if ( v ) {
75
- const buff = Buffer . from ( v , 'base64 ' ) ;
77
+ const buff = Buffer . from ( v , 'base64url ' ) ;
76
78
opt . text = buff . toString ( 'utf-8' ) ;
77
79
}
78
80
} else if ( k === 'image' ) {
79
81
if ( v ) {
80
- const buff = Buffer . from ( v , 'base64 ' ) ;
82
+ const buff = Buffer . from ( v , 'base64url ' ) ;
81
83
opt . image = buff . toString ( 'utf-8' ) ;
82
84
}
83
85
} else if ( k === 't' ) {
@@ -142,7 +144,7 @@ export class WatermarkAction extends BaseImageAction {
142
144
opt . color = v ;
143
145
} else if ( k === 'type' ) {
144
146
if ( v ) {
145
- const buff = Buffer . from ( v , 'base64 ' ) ;
147
+ const buff = Buffer . from ( v , 'base64url ' ) ;
146
148
opt . type = buff . toString ( 'utf-8' ) ;
147
149
}
148
150
} else if ( k === 'shadow' ) {
@@ -267,9 +269,10 @@ export class WatermarkAction extends BaseImageAction {
267
269
}
268
270
269
271
async textImg ( opt : WatermarkOpts ) : Promise < sharp . Sharp > {
272
+ const safetext = encode ( opt . text ) ;
270
273
const o = sharp ( {
271
274
text : {
272
- text : `<span size="${ opt . size } pt" foreground="#${ opt . color } ">${ opt . text } </span>` ,
275
+ text : `<span size="${ opt . size } pt" foreground="#${ opt . color } ">${ safetext } </span>` ,
273
276
align : 'center' ,
274
277
rgba : true ,
275
278
dpi : 72 ,
@@ -291,7 +294,7 @@ export class WatermarkAction extends BaseImageAction {
291
294
292
295
const shadow = sharp ( {
293
296
text : {
294
- text : `<span size="${ opt . size } pt" foreground="#${ opt . halo } ">${ opt . text } </span>` ,
297
+ text : `<span size="${ opt . size } pt" foreground="#${ opt . halo } ">${ safetext } </span>` ,
295
298
align : 'center' ,
296
299
rgba : true ,
297
300
dpi : 72 ,
@@ -300,15 +303,7 @@ export class WatermarkAction extends BaseImageAction {
300
303
301
304
const oBuffer = await o . png ( ) . toBuffer ( ) ;
302
305
const opacity = opt . shadow / 100 ;
303
- const copy = await shadow . convolve ( {
304
- width : 3 ,
305
- height : 3 ,
306
- kernel : [
307
- 0 , 0 , 0 ,
308
- 0 , opacity , 0 ,
309
- 0 , 0 , 0 ,
310
- ] ,
311
- } ) . png ( ) . toBuffer ( ) ;
306
+ const copy = await shadow . png ( ) . ensureAlpha ( opacity ) . toBuffer ( ) ;
312
307
313
308
const u = await sharp ( {
314
309
create : {
0 commit comments