@@ -35,19 +35,28 @@ export function applyHandler(
3535}
3636
3737export function clampDimensionsPreservingAspectRatio (
38+ fit : HandlerContext [ 'fit' ] ,
3839 sourceDimensions : ImageMeta ,
3940 desiredDimensions : { width : number ; height : number } ,
4041) {
4142 const desiredAspectRatio = desiredDimensions . width / desiredDimensions . height ;
43+ const sourceAspectRatio = sourceDimensions . width / sourceDimensions . height ;
4244 let { width, height } = desiredDimensions ;
4345 if ( sourceDimensions . width && width > sourceDimensions . width ) {
44- width = sourceDimensions . width ;
45- height = Math . round ( sourceDimensions . width / desiredAspectRatio ) ;
46+ if ( [ 'contain' , 'fill' , 'inside' ] . includes ( fit ) && sourceAspectRatio < desiredAspectRatio ) {
47+ width = Math . round ( height * desiredAspectRatio )
48+ } else {
49+ width = sourceDimensions . width ;
50+ height = Math . round ( sourceDimensions . width / desiredAspectRatio ) ;
51+ }
4652 }
4753 if ( sourceDimensions . height && height > sourceDimensions . height ) {
48- height = sourceDimensions . height ;
49- width = Math . round ( sourceDimensions . height * desiredAspectRatio ) ;
54+ if ( [ 'contain' , 'fill' , 'inside' ] . includes ( fit ) && sourceAspectRatio > desiredAspectRatio ) {
55+ height = Math . round ( width / desiredAspectRatio ) ;
56+ } else {
57+ height = sourceDimensions . height ;
58+ width = Math . round ( sourceDimensions . height * desiredAspectRatio ) ;
59+ }
5060 }
51-
5261 return { width, height } ;
5362}
0 commit comments