@@ -31,6 +31,8 @@ export interface IImageContext extends IProcessContext {
31
31
info ?: IImageInfo ;
32
32
}
33
33
34
+ const MB = 1024 * 1024 ;
35
+
34
36
export class ImageProcessor implements IProcessor {
35
37
public static getInstance ( ) : ImageProcessor {
36
38
if ( ! ImageProcessor . _instance ) {
@@ -77,15 +79,14 @@ export class ImageProcessor implements IProcessor {
77
79
if ( ctx . features [ Features . LimitAnimatedFrames ] > 0 ) {
78
80
image = sharp ( buffer , { failOnError : false , animated : false } ) ;
79
81
metadata = await image . metadata ( ) ;
80
- let cutGifFramesNum = ctx . features [ Features . LimitAnimatedFrames ] ;
81
82
if ( ! ( 'gif' === metadata . format ) ) {
82
83
throw new InvalidArgument ( 'Format must be Gif' ) ;
83
84
}
84
85
if ( ! ( metadata . pages ) ) {
85
86
throw new InvalidArgument ( 'Can\'t read gif\'s pages' ) ;
86
87
}
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 } ) ;
89
90
metadata = await image . metadata ( ) ;
90
91
} else {
91
92
image = sharp ( buffer , { failOnError : false , animated : ctx . features [ Features . ReadAllAnimatedFrames ] } ) ;
@@ -94,6 +95,9 @@ export class ImageProcessor implements IProcessor {
94
95
if ( 'gif' === metadata . format ) {
95
96
image . gif ( { effort : 1 } ) ; // https://github.com/lovell/sharp/issues/3176
96
97
}
98
+ if ( 'png' === metadata . format && metadata . size && metadata . size > ( 5 * MB ) ) {
99
+ image . png ( { adaptiveFiltering : true } ) ;
100
+ }
97
101
98
102
return {
99
103
uri : ctx . uri ,
0 commit comments