Skip to content

Commit d1036a9

Browse files
authored
Merge pull request #158 from wchaws/dev2
fix: disable gif enlarge and reduce high resolution png size
2 parents 54781b6 + 3d63e7e commit d1036a9

File tree

4 files changed

+75
-44
lines changed

4 files changed

+75
-44
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/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface IImageContext extends IProcessContext {
3131
info?: IImageInfo;
3232
}
3333

34+
const MB = 1024 * 1024;
35+
3436
export class ImageProcessor implements IProcessor {
3537
public static getInstance(): ImageProcessor {
3638
if (!ImageProcessor._instance) {
@@ -77,15 +79,14 @@ export class ImageProcessor implements IProcessor {
7779
if (ctx.features[Features.LimitAnimatedFrames] > 0) {
7880
image = sharp(buffer, { failOnError: false, animated: false });
7981
metadata = await image.metadata();
80-
let cutGifFramesNum = ctx.features[Features.LimitAnimatedFrames];
8182
if (!('gif' === metadata.format)) {
8283
throw new InvalidArgument('Format must be Gif');
8384
}
8485
if (!(metadata.pages)) {
8586
throw new InvalidArgument('Can\'t read gif\'s pages');
8687
}
87-
cutGifFramesNum = Math.min(cutGifFramesNum, metadata.pages);
88-
image = sharp(buffer, { failOnError: false, animated: ctx.features[Features.ReadAllAnimatedFrames], pages: cutGifFramesNum });
88+
const pages = Math.min(ctx.features[Features.LimitAnimatedFrames], metadata.pages);
89+
image = sharp(buffer, { failOnError: false, animated: ctx.features[Features.ReadAllAnimatedFrames], pages });
8990
metadata = await image.metadata();
9091
} else {
9192
image = sharp(buffer, { failOnError: false, animated: ctx.features[Features.ReadAllAnimatedFrames] });
@@ -94,6 +95,9 @@ export class ImageProcessor implements IProcessor {
9495
if ('gif' === metadata.format) {
9596
image.gif({ effort: 1 }); // https://github.com/lovell/sharp/issues/3176
9697
}
98+
if ('png' === metadata.format && metadata.size && metadata.size > (5 * MB)) {
99+
image.png({ adaptiveFiltering: true });
100+
}
97101

98102
return {
99103
uri: ctx.uri,

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ 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);
30-
const metadata = await ctx.image.metadata();
35+
const metadata = ctx.metadata;
3136
if (('jpg' === metadata.format || 'jpeg' === metadata.format) && opt.interlace) {
3237
ctx.image.jpeg({ progressive: true });
3338
}

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

+56-40
Original file line numberDiff line numberDiff line change
@@ -77,51 +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-
}
80+
81+
public beforeProcess(ctx: IImageContext, params: string[], index: number): void {
10082
const metadata = ctx.metadata;
101-
if (!(metadata.width && metadata.height)) {
102-
throw new InvalidArgument('Can\'t read image\'s width and height');
83+
if ('gif' === metadata.format) {
84+
const opt = buildSharpOpt(ctx, this.validate(params));
85+
const isEnlargingWidth = (opt.width && metadata.width && opt.width > metadata.width);
86+
const isEnlargingHeight = (opt.height && metadata.pageHeight && (opt.height > metadata.pageHeight));
87+
if (isEnlargingWidth || isEnlargingHeight) {
88+
ctx.mask.disable(index);
89+
}
10390
}
91+
}
10492

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-
}
93+
public async process(ctx: IImageContext, params: string[]): Promise<void> {
94+
const opt = buildSharpOpt(ctx, this.validate(params));
95+
ctx.image.resize(null, null, opt);
96+
}
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;
115132
}
116-
if (o.s) {
117-
if (metadata.height < metadata.width) {
118-
opt.height = o.s;
119-
} else {
120-
opt.width = o.s;
121-
}
133+
}
134+
if (o.s) {
135+
if (metadata.height < metadata.width) {
136+
opt.height = o.s;
137+
} else {
138+
opt.width = o.s;
122139
}
123140
}
124-
125-
ctx.image.resize(null, null, opt);
126141
}
142+
return opt;
127143
}

0 commit comments

Comments
 (0)