@@ -54,9 +54,7 @@ const getXY = index => {
5454 const isLeft = index % 2 === 0 ;
5555 // There is no margin between cards, instead, they are
5656 // separated by flex's space-between, which is directly
57- // affected by container width. When someday container
58- // width changes, we can just change its value and this
59- // method will be fixed.
57+ // affected by container width.
6058 const x = isLeft
6159 ? CARD_PADDING_HORIZONTAL
6260 : CONTAINER_WIDTH - ( CARD_PADDING_HORIZONTAL + CUT_WIDTH ) ;
@@ -96,19 +94,29 @@ const cropGif = async path => {
9694 try {
9795 const { name } = parse ( path ) ;
9896 const { width, height } = await sharp ( path ) . metadata ( ) ;
99- const resizeOpts =
100- width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
101- ? [ "--resize" , `_x${ MINIMUM_HEIGHT } ` ]
102- : [ "--resize" , `${ CONTAINER_WIDTH } x_` ] ;
97+ const dimension = width / height ;
98+ const BASE_DIMENSION = CONTAINER_WIDTH / MINIMUM_HEIGHT ;
99+ const isWider = dimension >= BASE_DIMENSION ;
100+
101+ const resizeOpts = isWider
102+ ? [ "--resize" , `_x${ MINIMUM_HEIGHT } ` ]
103+ : [ "--resize" , `${ CONTAINER_WIDTH } x_` ] ;
104+
103105 const resized = "resized.gif" ;
104106 await execa ( gifsicle , [ ...resizeOpts , "-o" , resized , path ] ) ;
107+ const { width : newWidth , height : newHeight } = await sharp (
108+ resized
109+ ) . metadata ( ) ;
110+
111+ const offsetX = ( newWidth - CONTAINER_WIDTH ) / 2 ;
112+ const offsetY = ( newHeight - MINIMUM_HEIGHT ) / 2 ;
105113 const files = [ ] ;
106114 for ( let i = 0 ; i < 6 ; i ++ ) {
107115 const filename = `${ name } .${ i } .gif` ;
108116 const { x, y } = getXY ( i ) ;
109117 await execa ( gifsicle , [
110118 "--crop" ,
111- `${ x } ,${ y } +${ CUT_WIDTH } x${ CUT_HEIGHT } ` ,
119+ `${ x + offsetX } ,${ y + offsetY } +${ CUT_WIDTH } x${ CUT_HEIGHT } ` ,
112120 "-o" ,
113121 filename ,
114122 resized
0 commit comments