Skip to content

Commit 7854851

Browse files
committed
fix: disable gif enlarge and reduce high resolution png size
0779ac46961491aaa1fa0a1fa3b48c79_6802770812466820349.gif?x-oss-process=image/resize,s_1000/quality,q_80/auto-orient,0/interlace,1/format,gif 1.PNG?x-oss-process=image/strip-metadata
1 parent 54781b6 commit 7854851

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class InterlaceAction extends BaseImageAction {
2727

2828
public async process(ctx: IImageContext, params: string[]): Promise<void> {
2929
const opt = this.validate(params);
30-
const metadata = await ctx.image.metadata();
30+
const metadata = ctx.metadata;
3131
if (('jpg' === metadata.format || 'jpeg' === metadata.format) && opt.interlace) {
3232
ctx.image.jpeg({ progressive: true });
3333
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ export class ResizeAction extends BaseImageAction {
122122
}
123123
}
124124

125+
if ('gif' === metadata.format) {
126+
const isEnlargingWidth = (opt.width && opt.width > metadata.width);
127+
const isEnlargingHeight = (opt.height && metadata.pageHeight && (opt.height > metadata.pageHeight));
128+
if (isEnlargingWidth || isEnlargingHeight) {
129+
return;
130+
}
131+
}
132+
125133
ctx.image.resize(null, null, opt);
126134
}
127135
}

0 commit comments

Comments
 (0)