@@ -54,12 +54,18 @@ const getXY = index => {
5454
5555const cropImage = async path => {
5656 try {
57- const [ ext , ...rest ] = path . split ( "." ) . reverse ( ) ;
58- const image = sharp ( path ) . resize ( CONTAINER_WIDTH ) ;
57+ const ext = path . split ( "." ) . reverse ( ) [ 0 ] ;
58+ const image = sharp ( path ) ;
59+ const { width, height } = await image . metadata ( ) ;
60+ const resizeOpts =
61+ width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
62+ ? { width : CONTAINER_WIDTH , height : MINIMUM_HEIGHT , fit : "outside" }
63+ : { width : CONTAINER_WIDTH } ;
64+ const resized = image . clone ( ) . resize ( resizeOpts ) ;
5965 for ( let i = 0 ; i < 6 ; i ++ ) {
6066 const filename = `${ i } .${ ext } ` ;
6167 const { x, y } = getXY ( i ) ;
62- await image
68+ await resized
6369 . clone ( )
6470 . extract ( { left : x , top : y , width : CUT_WIDTH , height : CUT_HEIGHT } )
6571 . toFile ( filename ) ;
@@ -71,15 +77,20 @@ const cropImage = async path => {
7177} ;
7278
7379const cropGif = async path => {
80+ const { width, height } = await sharp ( path ) . metadata ( ) ;
81+ const resizeOpts =
82+ width / height >= CONTAINER_WIDTH / MINIMUM_HEIGHT
83+ ? [ "--resize" , "_x513" ]
84+ : [ "--resize" , "727x_" ] ;
7485 const resized = "resized.gif" ;
75- await execa ( gifsicle , [ "--resize" , "727x_" , "-o" , resized , path ] ) ;
86+ await execa ( gifsicle , [ ... resizeOpts , "-o" , resized , path ] ) ;
7687 for ( let i = 0 ; i < 6 ; i ++ ) {
7788 const filename = `${ i } .gif` ;
7889 const { x, y } = getXY ( i ) ;
7990 await execa ( gifsicle , [
8091 "--crop" ,
8192 `${ x } ,${ y } +${ CUT_WIDTH } x${ CUT_HEIGHT } ` ,
82- "--output " ,
93+ "-o " ,
8394 filename ,
8495 resized
8596 ] ) ;
0 commit comments