@@ -22,13 +22,16 @@ export interface ImageProps extends Omit<HTMLAttributes<'img'>, 'src'> {
22
22
widths ?: number [ ] | null ;
23
23
aspectRatio ?: string | number | null ;
24
24
objectPosition ?: string ;
25
+
26
+ format ?: string ;
25
27
}
26
28
27
29
export type ImagesOptimizer = (
28
30
image : ImageMetadata | string ,
29
31
breakpoints : number [ ] ,
30
32
width ?: number ,
31
- height ?: number
33
+ height ?: number ,
34
+ format ?: string
32
35
) => Promise < Array < { src : string ; width : number } > > ;
33
36
34
37
/* ******* */
@@ -209,17 +212,25 @@ const getBreakpoints = ({
209
212
} ;
210
213
211
214
/* ** */
212
- export const astroAsseetsOptimizer : ImagesOptimizer = async ( image , breakpoints , _width , _height ) => {
215
+ export const astroAsseetsOptimizer : ImagesOptimizer = async (
216
+ image ,
217
+ breakpoints ,
218
+ _width ,
219
+ _height ,
220
+ format = undefined
221
+ ) => {
213
222
if ( ! image ) {
214
223
return [ ] ;
215
224
}
216
225
217
226
return Promise . all (
218
227
breakpoints . map ( async ( w : number ) => {
219
- const url = ( await getImage ( { src : image , width : w , inferSize : true } ) ) . src ;
228
+ const result = ( await getImage ( { src : image , width : w , inferSize : true , ...( format ? { format : format } : { } ) } ) ) ;
229
+
220
230
return {
221
- src : url ,
222
- width : w ,
231
+ src : result ?. src ,
232
+ width : result ?. attributes ?. width ?? w ,
233
+ height : result ?. attributes ?. height
223
234
} ;
224
235
} )
225
236
) ;
@@ -230,7 +241,7 @@ export const isUnpicCompatible = (image: string) => {
230
241
} ;
231
242
232
243
/* ** */
233
- export const unpicOptimizer : ImagesOptimizer = async ( image , breakpoints , width , height ) => {
244
+ export const unpicOptimizer : ImagesOptimizer = async ( image , breakpoints , width , height , format = undefined ) => {
234
245
if ( ! image || typeof image !== 'string' ) {
235
246
return [ ] ;
236
247
}
@@ -242,16 +253,19 @@ export const unpicOptimizer: ImagesOptimizer = async (image, breakpoints, width,
242
253
243
254
return Promise . all (
244
255
breakpoints . map ( async ( w : number ) => {
256
+ const _height = width && height ? computeHeight ( w , width / height ) : height ;
245
257
const url =
246
258
transformUrl ( {
247
259
url : image ,
248
260
width : w ,
249
- height : width && height ? computeHeight ( w , width / height ) : height ,
261
+ height : _height ,
250
262
cdn : urlParsed . cdn ,
263
+ ...( format ? { format : format } : { } ) ,
251
264
} ) || image ;
252
265
return {
253
266
src : String ( url ) ,
254
267
width : w ,
268
+ height : _height ,
255
269
} ;
256
270
} )
257
271
) ;
@@ -270,6 +284,7 @@ export async function getImagesOptimized(
270
284
widths,
271
285
layout = 'constrained' ,
272
286
style = '' ,
287
+ format,
273
288
...rest
274
289
} : ImageProps ,
275
290
transform : ImagesOptimizer = ( ) => Promise . resolve ( [ ] )
@@ -312,7 +327,7 @@ export async function getImagesOptimized(
312
327
let breakpoints = getBreakpoints ( { width : width , breakpoints : widths , layout : layout } ) ;
313
328
breakpoints = [ ...new Set ( breakpoints ) ] . sort ( ( a , b ) => a - b ) ;
314
329
315
- const srcset = ( await transform ( image , breakpoints , Number ( width ) || undefined , Number ( height ) || undefined ) )
330
+ const srcset = ( await transform ( image , breakpoints , Number ( width ) || undefined , Number ( height ) || undefined , format ) )
316
331
. map ( ( { src, width } ) => `${ src } ${ width } w` )
317
332
. join ( ', ' ) ;
318
333
0 commit comments