@@ -83,19 +83,39 @@ function (array $context, string $path, array $options = []): string {
8383 $ srcsetFallback = $ srcFallback1x .' 1x ' .($ srcFallback2x ? ', ' .$ srcFallback2x .' 2x ' : '' );
8484
8585 // Build <picture> element
86- $ html = '<picture> ' ;
86+ $ attributes = array_filter ([
87+ 'class ' => $ options ['attributes ' ]['class ' ] ?? null ,
88+ 'style ' => $ options ['attributes ' ]['style ' ] ?? null ,
89+ ]);
90+
91+ $ imgAttributes = array_filter ([
92+ 'src ' => $ srcFallback1x ,
93+ 'srcset ' => $ srcsetFallback ,
94+ 'width ' => $ width !== null ? (string ) $ width : null ,
95+ 'height ' => $ height !== null ? (string ) $ height : null ,
96+ 'alt ' => $ options ['attributes ' ]['alt ' ] ?? '' ,
97+ 'loading ' => $ options ['attributes ' ]['loading ' ] ?? 'lazy ' ,
98+ 'decoding ' => $ options ['attributes ' ]['decoding ' ] ?? 'async ' ,
99+ ]);
100+
101+ $ html = '<picture ' .implode (' ' , array_map (
102+ static fn ($ key , $ value ) => htmlspecialchars ($ key , \ENT_QUOTES ).'=" ' .htmlspecialchars ($ value , \ENT_QUOTES ).'" ' ,
103+ array_keys ($ attributes ),
104+ $ attributes
105+ )).'> ' ;
87106 $ html .= '<source type="image/avif" srcset=" ' .htmlspecialchars ($ srcsetAvif , \ENT_QUOTES ).'"/> ' ;
88107 $ html .= '<source type="image/webp" srcset=" ' .htmlspecialchars ($ srcsetWebp , \ENT_QUOTES ).'"/> ' ;
89108 if ($ fallbackFormat === 'png ' ) {
90109 $ html .= '<source type="image/png" srcset=" ' .htmlspecialchars ($ srcsetFallback , \ENT_QUOTES ).'"/> ' ;
91110 } else {
92111 $ html .= '<source type="image/jpeg" srcset=" ' .htmlspecialchars ($ srcsetFallback , \ENT_QUOTES ).'"/> ' ;
93112 }
94- $ html .= '<img src=" ' .htmlspecialchars ($ srcFallback1x , \ENT_QUOTES ).'" '
95- .' srcset=" ' .htmlspecialchars ($ srcsetFallback , \ENT_QUOTES ).'" '
96- .($ width !== null ? ' width=" ' .$ width .'" ' : '' )
97- .($ height !== null ? ' height=" ' .$ height .'" ' : '' )
98- .' loading="lazy" decoding="async" /> ' ;
113+ $ html .= '<img '
114+ .implode (' ' , array_map (
115+ static fn ($ key , $ value ) => htmlspecialchars ($ key , \ENT_QUOTES ).'=" ' .htmlspecialchars ($ value , \ENT_QUOTES ).'" ' ,
116+ array_keys ($ imgAttributes ),
117+ $ imgAttributes
118+ )).' /> ' ;
99119 $ html .= '</picture> ' ;
100120
101121 return $ html ;
@@ -184,9 +204,7 @@ private function buildOriginAbsolutePath(string $path, array $context, array $op
184204 private function buildImgproxyFilter (array $ options ): string
185205 {
186206 $ filter = '' ;
187- if (isset ($ options ['self ' ])) {
188- unset($ options ['self ' ]);
189- }
207+ unset($ options ['self ' ], $ options ['attributes ' ]);
190208
191209 if ($ options !== []) {
192210 $ filters = [];
0 commit comments