@@ -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