|
| 1 | +# Example: JSON Output |
| 2 | + |
| 3 | +The `imagex-picture.json` snippet provides JSON output instead of HTML markup. This is useful for: |
| 4 | +- Headless CMS setups |
| 5 | +- API endpoints |
| 6 | +- JavaScript-driven rendering (SPA, React, Vue, etc.) |
| 7 | +- Custom client-side image handling |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +The `imagex-picture.json` snippet accepts the same options as `imagex-picture` but returns JSON data instead of HTML. |
| 12 | + |
| 13 | +### Global Plugin Config |
| 14 | +```php |
| 15 | +// config.php |
| 16 | +return [ |
| 17 | + 'timnarr.imagex' => [ |
| 18 | + 'cache' => true, |
| 19 | + 'customLazyloading' => false, |
| 20 | + 'formats' => ['avif', 'webp'], |
| 21 | + 'includeInitialFormat' => false, |
| 22 | + 'noSrcsetInImg' => false, |
| 23 | + 'relativeUrls' => false, |
| 24 | + ], |
| 25 | +]; |
| 26 | +``` |
| 27 | + |
| 28 | +### Snippet Options |
| 29 | +```php |
| 30 | +<?php |
| 31 | +$options = [ |
| 32 | + 'image' => $image->toFile(), // let's assume: `image.jpg` with 16/9 aspect ratio |
| 33 | + 'srcsetName' => 'imagex-demo', |
| 34 | + 'ratio' => '16/9', |
| 35 | + 'imgAttributes' => [ |
| 36 | + 'shared' => [ |
| 37 | + 'alt' => $image->toFile()->alt(), |
| 38 | + 'sizes' => '(min-width: 800px) 400px, 100vw', |
| 39 | + 'class' => 'my-image', |
| 40 | + ], |
| 41 | + ], |
| 42 | + 'pictureAttributes' => [ |
| 43 | + 'shared' => [ |
| 44 | + 'class' => 'my-picture', |
| 45 | + 'data-component' => 'responsive-image' |
| 46 | + ] |
| 47 | + ] |
| 48 | +]; |
| 49 | + |
| 50 | +// Important: Pass `true` as third parameter to return the snippet output |
| 51 | +$json = snippet('imagex-picture.json', $options, true); |
| 52 | + |
| 53 | +// Output JSON |
| 54 | +header('Content-Type: application/json'); |
| 55 | +echo $json; |
| 56 | + |
| 57 | +// Or decode and process the data |
| 58 | +$data = json_decode($json, true); |
| 59 | +?> |
| 60 | +``` |
| 61 | + |
| 62 | +### JSON Output |
| 63 | +```json |
| 64 | +{ |
| 65 | + "pictureAttributes": { |
| 66 | + "class": "my-picture", |
| 67 | + "data-component": "responsive-image" |
| 68 | + }, |
| 69 | + "sources": [ |
| 70 | + { |
| 71 | + "width": 400, |
| 72 | + "height": 225, |
| 73 | + "type": "image/avif", |
| 74 | + "srcset": "https://example.com/image-400x225-crop-52-65-q65-sharpen25.avif 400w, https://example.com/image-800x450-crop-52-65-q65-sharpen25.avif 800w" |
| 75 | + }, |
| 76 | + { |
| 77 | + "width": 400, |
| 78 | + "height": 225, |
| 79 | + "type": "image/webp", |
| 80 | + "srcset": "https://example.com/image-400x225-crop-52-65-q75-sharpen10.webp 400w, https://example.com/image-800x450-crop-52-65-q75-sharpen10.webp 800w" |
| 81 | + } |
| 82 | + ], |
| 83 | + "imgAttributes": { |
| 84 | + "class": "my-image", |
| 85 | + "sizes": "(min-width: 800px) 400px, 100vw", |
| 86 | + "alt": "A cat sits in the sun in front of yellow flowers.", |
| 87 | + "width": 400, |
| 88 | + "height": 225, |
| 89 | + "decoding": "async", |
| 90 | + "loading": "lazy", |
| 91 | + "src": "https://example.com/image-400x225-crop-52-65-q80-sharpen10.jpg", |
| 92 | + "srcset": "https://example.com/image-400x225-crop-52-65-q80-sharpen10.jpg 400w, https://example.com/image-800x450-crop-52-65-q80-sharpen10.jpg 800w" |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Notes |
| 98 | + |
| 99 | +- All options from `imagex-picture` work with `imagex-picture.json` |
| 100 | +- The third parameter (`true`) in `snippet('imagex-picture.json', $options, true)` is required to return the output instead of echoing it |
| 101 | +- The JSON includes all calculated attributes, srcsets, and URLs |
| 102 | +- Art-directed sources are also included in the `sources` array with their media queries |
| 103 | +- Combine with Kirby's routing system to create custom API endpoints |
0 commit comments