Skip to content

Commit 3d63e7e

Browse files
committed
chore: optimze for gif if no need to process
1 parent 7854851 commit 3d63e7e

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

source/new-image-handler/src/processor/image/auto-orient.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export class AutoOrientAction extends BaseImageAction {
1313
ctx.features[Features.AutoOrient] = false;
1414
}
1515

16+
public beforeProcess(ctx: IImageContext, _2: string[], index: number): void {
17+
if ('gif' === ctx.metadata.format) {
18+
ctx.mask.disable(index);
19+
}
20+
}
21+
1622
public validate(params: string[]): ReadOnly<AutoOrientOpts> {
1723
const opt: AutoOrientOpts = { auto: false };
1824

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

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export class InterlaceAction extends BaseImageAction {
2424
return opt;
2525
}
2626

27+
public beforeProcess(ctx: IImageContext, _2: string[], index: number): void {
28+
if ('gif' === ctx.metadata.format) {
29+
ctx.mask.disable(index);
30+
}
31+
}
2732

2833
public async process(ctx: IImageContext, params: string[]): Promise<void> {
2934
const opt = this.validate(params);

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

+54-46
Original file line numberDiff line numberDiff line change
@@ -77,59 +77,67 @@ export class ResizeAction extends BaseImageAction {
7777
}
7878
return opt;
7979
}
80-
public async process(ctx: IImageContext, params: string[]): Promise<void> {
81-
const o = this.validate(params);
82-
const opt: sharp.ResizeOptions = {
83-
width: o.w,
84-
height: o.h,
85-
withoutEnlargement: o.limit,
86-
background: o.color,
87-
};
88-
// Mode
89-
if (o.m === Mode.LFIT) {
90-
opt.fit = sharp.fit.inside;
91-
} else if (o.m === Mode.MFIT) {
92-
opt.fit = sharp.fit.outside;
93-
} else if (o.m === Mode.FILL) {
94-
opt.fit = sharp.fit.cover;
95-
} else if (o.m === Mode.PAD) {
96-
opt.fit = sharp.fit.contain;
97-
} else if (o.m === Mode.FIXED) {
98-
opt.fit = sharp.fit.fill;
99-
}
100-
const metadata = ctx.metadata;
101-
if (!(metadata.width && metadata.height)) {
102-
throw new InvalidArgument('Can\'t read image\'s width and height');
103-
}
104-
105-
if (o.p && (!o.w) && (!o.h)) {
106-
opt.withoutEnlargement = false;
107-
opt.width = Math.round(metadata.width * o.p * 0.01);
108-
} else {
109-
if (o.l) {
110-
if (metadata.width > metadata.height) {
111-
opt.width = o.l;
112-
} else {
113-
opt.height = o.l;
114-
}
115-
}
116-
if (o.s) {
117-
if (metadata.height < metadata.width) {
118-
opt.height = o.s;
119-
} else {
120-
opt.width = o.s;
121-
}
122-
}
123-
}
12480

81+
public beforeProcess(ctx: IImageContext, params: string[], index: number): void {
82+
const metadata = ctx.metadata;
12583
if ('gif' === metadata.format) {
126-
const isEnlargingWidth = (opt.width && opt.width > metadata.width);
84+
const opt = buildSharpOpt(ctx, this.validate(params));
85+
const isEnlargingWidth = (opt.width && metadata.width && opt.width > metadata.width);
12786
const isEnlargingHeight = (opt.height && metadata.pageHeight && (opt.height > metadata.pageHeight));
12887
if (isEnlargingWidth || isEnlargingHeight) {
129-
return;
88+
ctx.mask.disable(index);
13089
}
13190
}
91+
}
13292

93+
public async process(ctx: IImageContext, params: string[]): Promise<void> {
94+
const opt = buildSharpOpt(ctx, this.validate(params));
13395
ctx.image.resize(null, null, opt);
13496
}
97+
}
98+
99+
function buildSharpOpt(ctx: IImageContext, o: ResizeOpts): sharp.ResizeOptions {
100+
const opt: sharp.ResizeOptions = {
101+
width: o.w,
102+
height: o.h,
103+
withoutEnlargement: o.limit,
104+
background: o.color,
105+
};
106+
// Mode
107+
if (o.m === Mode.LFIT) {
108+
opt.fit = sharp.fit.inside;
109+
} else if (o.m === Mode.MFIT) {
110+
opt.fit = sharp.fit.outside;
111+
} else if (o.m === Mode.FILL) {
112+
opt.fit = sharp.fit.cover;
113+
} else if (o.m === Mode.PAD) {
114+
opt.fit = sharp.fit.contain;
115+
} else if (o.m === Mode.FIXED) {
116+
opt.fit = sharp.fit.fill;
117+
}
118+
const metadata = ctx.metadata;
119+
if (!(metadata.width && metadata.height)) {
120+
throw new InvalidArgument('Can\'t read image\'s width and height');
121+
}
122+
123+
if (o.p && (!o.w) && (!o.h)) {
124+
opt.withoutEnlargement = false;
125+
opt.width = Math.round(metadata.width * o.p * 0.01);
126+
} else {
127+
if (o.l) {
128+
if (metadata.width > metadata.height) {
129+
opt.width = o.l;
130+
} else {
131+
opt.height = o.l;
132+
}
133+
}
134+
if (o.s) {
135+
if (metadata.height < metadata.width) {
136+
opt.height = o.s;
137+
} else {
138+
opt.width = o.s;
139+
}
140+
}
141+
}
142+
return opt;
135143
}

0 commit comments

Comments
 (0)