@@ -22,13 +22,16 @@ export interface ImageProps extends Omit<HTMLAttributes<'img'>, 'src'> {
2222 widths ?: number [ ] | null ;
2323 aspectRatio ?: string | number | null ;
2424 objectPosition ?: string ;
25+
26+ format ?: string ;
2527}
2628
2729export type ImagesOptimizer = (
2830 image : ImageMetadata | string ,
2931 breakpoints : number [ ] ,
3032 width ?: number ,
31- height ?: number
33+ height ?: number ,
34+ format ?: string
3235) => Promise < Array < { src : string ; width : number } > > ;
3336
3437/* ******* */
@@ -209,17 +212,25 @@ const getBreakpoints = ({
209212} ;
210213
211214/* ** */
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+ ) => {
213222 if ( ! image ) {
214223 return [ ] ;
215224 }
216225
217226 return Promise . all (
218227 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+
220230 return {
221- src : url ,
222- width : w ,
231+ src : result ?. src ,
232+ width : result ?. attributes ?. width ?? w ,
233+ height : result ?. attributes ?. height
223234 } ;
224235 } )
225236 ) ;
@@ -230,7 +241,7 @@ export const isUnpicCompatible = (image: string) => {
230241} ;
231242
232243/* ** */
233- export const unpicOptimizer : ImagesOptimizer = async ( image , breakpoints , width , height ) => {
244+ export const unpicOptimizer : ImagesOptimizer = async ( image , breakpoints , width , height , format = undefined ) => {
234245 if ( ! image || typeof image !== 'string' ) {
235246 return [ ] ;
236247 }
@@ -242,16 +253,19 @@ export const unpicOptimizer: ImagesOptimizer = async (image, breakpoints, width,
242253
243254 return Promise . all (
244255 breakpoints . map ( async ( w : number ) => {
256+ const _height = width && height ? computeHeight ( w , width / height ) : height ;
245257 const url =
246258 transformUrl ( {
247259 url : image ,
248260 width : w ,
249- height : width && height ? computeHeight ( w , width / height ) : height ,
261+ height : _height ,
250262 cdn : urlParsed . cdn ,
263+ ...( format ? { format : format } : { } ) ,
251264 } ) || image ;
252265 return {
253266 src : String ( url ) ,
254267 width : w ,
268+ height : _height ,
255269 } ;
256270 } )
257271 ) ;
@@ -270,6 +284,7 @@ export async function getImagesOptimized(
270284 widths,
271285 layout = 'constrained' ,
272286 style = '' ,
287+ format,
273288 ...rest
274289 } : ImageProps ,
275290 transform : ImagesOptimizer = ( ) => Promise . resolve ( [ ] )
@@ -312,7 +327,7 @@ export async function getImagesOptimized(
312327 let breakpoints = getBreakpoints ( { width : width , breakpoints : widths , layout : layout } ) ;
313328 breakpoints = [ ...new Set ( breakpoints ) ] . sort ( ( a , b ) => a - b ) ;
314329
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 ) )
316331 . map ( ( { src, width } ) => `${ src } ${ width } w` )
317332 . join ( ', ' ) ;
318333
0 commit comments