Skip to content

Commit 54781b6

Browse files
authored
Merge pull request #157 from tingxin/fix_watermark_malformed
fix text decode issue
2 parents c579530 + 92d460e commit 54781b6

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

source/new-image-handler/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"dependencies": {
4949
"aws-sdk": "^2.1130.0",
50+
"html-entities": "^2.3.3",
5051
"http-errors": "^1.8.1",
5152
"koa": "^2.13.4",
5253
"koa-bodyparser": "^4.3.0",

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

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { encode } from 'html-entities';
12
import * as sharp from 'sharp';
23
import { IImageContext } from '.';
34
import { IActionOpts, ReadOnly, InvalidArgument, Features, IProcessContext } from '..';
45
import * as is from '../../is';
56
import { BaseImageAction, split1 } from './_base';
67

8+
79
export interface WatermarkOpts extends IActionOpts {
810
text: string;
911
t: number; // 不透明度
@@ -72,12 +74,12 @@ export class WatermarkAction extends BaseImageAction {
7274
const [k, v] = split1(param, '_');
7375
if (k === 'text') {
7476
if (v) {
75-
const buff = Buffer.from(v, 'base64');
77+
const buff = Buffer.from(v, 'base64url');
7678
opt.text = buff.toString('utf-8');
7779
}
7880
} else if (k === 'image') {
7981
if (v) {
80-
const buff = Buffer.from(v, 'base64');
82+
const buff = Buffer.from(v, 'base64url');
8183
opt.image = buff.toString('utf-8');
8284
}
8385
} else if (k === 't') {
@@ -142,7 +144,7 @@ export class WatermarkAction extends BaseImageAction {
142144
opt.color = v;
143145
} else if (k === 'type') {
144146
if (v) {
145-
const buff = Buffer.from(v, 'base64');
147+
const buff = Buffer.from(v, 'base64url');
146148
opt.type = buff.toString('utf-8');
147149
}
148150
} else if (k === 'shadow') {
@@ -267,9 +269,10 @@ export class WatermarkAction extends BaseImageAction {
267269
}
268270

269271
async textImg(opt: WatermarkOpts): Promise<sharp.Sharp> {
272+
const safetext = encode(opt.text);
270273
const o = sharp({
271274
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>`,
273276
align: 'center',
274277
rgba: true,
275278
dpi: 72,
@@ -291,7 +294,7 @@ export class WatermarkAction extends BaseImageAction {
291294

292295
const shadow = sharp({
293296
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>`,
295298
align: 'center',
296299
rgba: true,
297300
dpi: 72,
@@ -300,15 +303,7 @@ export class WatermarkAction extends BaseImageAction {
300303

301304
const oBuffer = await o.png().toBuffer();
302305
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();
312307

313308
const u = await sharp({
314309
create: {

source/new-image-handler/yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3178,6 +3178,11 @@ html-encoding-sniffer@^2.0.1:
31783178
dependencies:
31793179
whatwg-encoding "^1.0.5"
31803180

3181+
html-entities@^2.3.3:
3182+
version "2.3.3"
3183+
resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-2.3.3.tgz#117d7626bece327fc8baace8868fa6f5ef856e46"
3184+
integrity sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA==
3185+
31813186
html-escaper@^2.0.0:
31823187
version "2.0.2"
31833188
resolved "https://registry.yarnpkg.com/html-escaper/-/html-escaper-2.0.2.tgz#dfd60027da36a36dfcbe236262c00a5822681453"

0 commit comments

Comments
 (0)