@@ -220,8 +220,9 @@ const service: LocalImageService = {
220220 inferSize ?: boolean ;
221221 }
222222 ) : Promise < { data : Uint8Array ; format : ImageOutputFormat } > {
223+ const sharp = ( await import ( "sharp" ) ) . default ;
223224 const MAX_WIDTH = 1280 ;
224- const MAX_HEIGHT = 720 ;
225+ const MAX_HEIGHT = 1280 ;
225226 let buffer = inputBuffer ;
226227
227228 if ( / ^ h t t p s ? : \/ \/ / . test ( transform . src ) ) {
@@ -236,30 +237,51 @@ const service: LocalImageService = {
236237 }
237238 }
238239
239- let width = transform . width
240- ? Math . min ( transform . width , MAX_WIDTH )
241- : undefined ;
242- let height = transform . height
243- ? Math . min ( transform . height , MAX_HEIGHT )
244- : undefined ;
240+ let width = transform . width ;
241+ let height = transform . height ;
245242
246- if ( ! transform . width || ! transform . height ) {
247- try {
248- const sharp = ( await import ( "sharp" ) ) . default ;
249- const metadata = await sharp ( buffer ) . metadata ( ) ;
250- if ( ! width && metadata . width ) {
251- width = metadata . width ;
252- }
253- if ( ! height && metadata . height ) {
254- height = metadata . height ;
243+ try {
244+ const metadata = await sharp ( buffer ) . metadata ( ) ;
245+
246+ width = metadata . width ;
247+ height = metadata . height ;
248+
249+ // Ensure width and height are both defined before calculating aspect ratio
250+ if ( metadata . width && metadata . height ) {
251+ const aspectRatio = metadata . width / metadata . height ;
252+
253+ // Resize the image while maintaining the aspect ratio and ensuring it fits within the 1280x1280 square
254+ if ( width ) {
255+ // If only width is defined, calculate the height to maintain the aspect ratio
256+ height = Math . round ( width / aspectRatio ) ;
257+ if ( height > MAX_HEIGHT ) {
258+ height = MAX_HEIGHT ;
259+ width = Math . round ( height * aspectRatio ) ;
260+ }
261+ } else if ( height ) {
262+ // If only height is defined, calculate the width to maintain the aspect ratio
263+ width = Math . round ( height * aspectRatio ) ;
264+ if ( width > MAX_WIDTH ) {
265+ width = MAX_WIDTH ;
266+ height = Math . round ( width / aspectRatio ) ;
267+ }
268+ } else {
269+ // If neither width nor height is defined, ensure the image fits within the max width and height
270+ if ( metadata . width > metadata . height ) {
271+ width = Math . min ( metadata . width , MAX_WIDTH ) ;
272+ height = Math . round ( width / aspectRatio ) ;
273+ } else {
274+ height = Math . min ( metadata . height , MAX_HEIGHT ) ;
275+ width = Math . round ( height * aspectRatio ) ;
276+ }
255277 }
256- } catch ( err ) {
257- console . warn ( `Error inferring image size: ${ err } ` ) ;
258278 }
279+ } catch ( err ) {
280+ console . warn ( `Error inferring image size: ${ err } ` ) ;
259281 }
260282
283+ // Proceed with resizing and transformation
261284 try {
262- const sharp = ( await import ( "sharp" ) ) . default ;
263285 let image = sharp ( buffer , { failOn : "none" } ) ;
264286 image = image . resize ( width , height ) ;
265287
@@ -277,6 +299,7 @@ const service: LocalImageService = {
277299 } catch ( err ) {
278300 console . warn ( `⚠️ Error processing image: ${ transform . src } ` , err ) ;
279301
302+ // Handle fallback
280303 try {
281304 const placeholderBuffer = await createPlaceholderImage (
282305 transform . width || 400 ,
0 commit comments