You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+128-2Lines changed: 128 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -147,6 +147,10 @@ You can choose from many options to customize your images and pass them to the I
147
147
148
148
For each HTML element of the picture element you can add attributes, CSS classes, inline-styles, data-attributes and so on. You only need to add your attributes to one of these three attribute categories, which I call "loading modes": `shared`, `eager` and `lazy`. The `shared` mode is for attributes that should exists always, no matter what loading mode the image is. If you have attributes that should only be used in `eager` or `lazy` loading mode you can add them to one of it. Imagex will merge the `shared` attributes with the attributes of the current loading mode automatically. The attributes of the non-applicable loading mode will have no effect then.
149
149
150
+
**All attributes can be overridden:** User-defined attributes always take precedence over default attributes generated by Imagex. This means you can override any attribute, including `src`, `srcset`, `loading`, `width`, `height`, and others. If you don't specify an attribute, Imagex will use its default values as fallback. For `class` and `style` attributes, user values are merged with defaults rather than replaced.
151
+
152
+
⚠️ **Note:** When overriding dimension-related attributes (`width`, `height`, `src`, `srcset`), be aware that Imagex calculates dimensions based on the `ratio` parameter. Overriding these attributes makes you responsible for maintaining consistency between dimensions and image sources. [See detailed explanation below](#overriding-default-attributes).
153
+
150
154
| Option | Default | Type | Description |
151
155
| ------ | ------- | ---- | ----------- |
152
156
|`image`| – | File-Object | Required. Your initial image. Be sure to return a file object here: `$field->toFile()`. |
@@ -195,8 +199,8 @@ $options = [
195
199
'lazy' => [
196
200
// extend `shared` attributes in lazy loading mode
197
201
],
198
-
// Do not add `src`, `srcset` or `loading` or their equivalents for lazy loading (like `data-src`) here.
199
-
// These attributes are handled automatically by Imagex and adding them here will throw an exception.
202
+
// You can override any attribute generated by Imagex, including `src`, `srcset`, `loading`, etc.
203
+
// User-defined attributes always take precedence over Imagex defaults.
200
204
],
201
205
'srcsetName' => 'my-srcset',
202
206
'critical' => $isCritical ?? false,
@@ -213,6 +217,128 @@ $options = [
213
217
<?php snippet('imagex-picture', $options) ?>
214
218
```
215
219
220
+
### Overriding Default Attributes
221
+
You can override any attribute that Imagex generates by default. User-defined attributes always take precedence.
222
+
223
+
#### Why Override Attributes?
224
+
225
+
While Imagex handles most scenarios automatically, there are practical reasons to override certain attributes:
226
+
227
+
**1. Improved SEO and Social Media Sharing**
228
+
Many crawlers (Google, Facebook, Twitter) don't fully understand `<picture>` elements or modern formats and only read the `src` attribute of the `<img>` tag. By default, Imagex uses the smallest image from your srcset as `src` (e.g., 400px width). Overriding `src` with a larger, high-quality image ensures better:
229
+
- Google Image Search results
230
+
- Social media previews (Open Graph, Twitter Cards)
231
+
- SEO rankings with higher-quality images
232
+
233
+
**2. Custom Lazy Loading Strategies**
234
+
Override `src` with a low-quality image placeholder (LQIP) or blur-up effect for custom lazy loading implementations.
235
+
236
+
**3. Special Loading Behavior**
237
+
Override `loading` or other attributes for specific images that need different behavior than the global settings.
-`width` and `height` will be set to your custom values instead of the calculated defaults
263
+
-`src` will use your custom placeholder image
264
+
-`loading` attribute will be set to `eager` even though Imagex would normally set it to `lazy`
265
+
- Attributes not specified by you (like `decoding`, `fetchpriority`, etc.) will still use Imagex defaults
266
+
267
+
#### SEO-Optimized Example
268
+
269
+
```php
270
+
<?php
271
+
// Provide a larger image for crawlers while keeping optimized srcset for browsers
272
+
$options = [
273
+
'image' => $image,
274
+
'imgAttributes' => [
275
+
'shared' => [
276
+
'src' => $image->thumb(['width' => 1200, 'quality' => 85])->url(), // Large image for crawlers
277
+
'alt' => $image->alt(),
278
+
],
279
+
],
280
+
'srcsetName' => 'my-srcset', // Srcset will still use optimized sizes (400w, 800w, etc.)
281
+
];
282
+
283
+
snippet('imagex-picture', $options);
284
+
```
285
+
286
+
In this SEO example:
287
+
- Modern browsers use the optimized `srcset` with modern formats (avif, webp) and appropriate sizes
288
+
- Crawlers and social media bots get a high-quality 1200px image from the `src` attribute (which serves as a fallback)
289
+
- Best of both worlds: Fast loading for users, quality images for SEO
290
+
291
+
**Note on src as fallback:** The `src` attribute primarily serves as a fallback for very old browsers and is what crawlers read. Modern browsers will always prefer images from `srcset` or `<source>` elements. This means you can safely override `src` with a different aspect ratio or larger size for SEO purposes without affecting the user experience, as browsers will load the correctly-sized images from your `srcset`.
292
+
293
+
**Example - Different ratio for SEO:**
294
+
```php
295
+
<?php
296
+
// Use 16:9 for browsers, but 1:1 for SEO/social media
297
+
$options = [
298
+
'image' => $image,
299
+
'ratio' => '16/9', // Browser gets 16:9 images via srcset
Result: Browsers display 16:9 images (from srcset), crawlers and social media get 1:1 image (from src).
310
+
311
+
**Important Note on Ratio Handling:**
312
+
Imagex automatically calculates image dimensions based on the specified `ratio` parameter. When you override dimension-related attributes (`width`, `height`, `src`, or `srcset`), you become responsible for maintaining consistency:
313
+
314
+
-**Overriding `width`/`height`**: The `srcset` will still use thumbnails generated with the original ratio, which may not match your custom dimensions.
315
+
-**Overriding `src`/`srcset`**: The `width` and `height` attributes will still reflect the calculated ratio, which may not match your custom images.
316
+
317
+
If you override these attributes, ensure that your custom values are consistent with each other and with the aspect ratio of your images to avoid layout shifts or distorted images.
318
+
319
+
**Example - Correct way to override dimensions:**
320
+
```php
321
+
<?php
322
+
// If you need custom dimensions, override all related attributes together
323
+
$options = [
324
+
'image' => $image,
325
+
'imgAttributes' => [
326
+
'shared' => [
327
+
'width' => 600,
328
+
'height' => 400, // Make sure this matches your desired ratio (3:2 in this case)
329
+
],
330
+
'lazy' => [
331
+
// If you also override srcset, make sure your images have the same 3:2 ratio
'ratio' => '3/2', // This ratio is now only used for generating the default thumbnails
337
+
];
338
+
```
339
+
Note: In most cases, you should **not** need to override `width`, `height`, or `srcset`. Let Imagex handle these automatically based on your `ratio` parameter for best results.
340
+
```
341
+
216
342
## Cache
217
343
Imagex will do some simple calculations per image, like calculating the height by the given width and ratio. Basically Imagex get the srcset definition from the config file, calculate and set the height and output the final config. The result will be cached to reduce unnecessary calculations when you use the same combination of srcset-preset and ratio for other images.
0 commit comments