Skip to content

Commit e37e5d8

Browse files
committed
feat: add snippet to provide JSON output instead of HTML
1 parent 9b35192 commit e37e5d8

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ $options = [
142142
</figure>
143143
```
144144

145+
146+
### JSON Output
147+
If you need JSON output instead of HTML markup (for headless CMS setups, API endpoints, or JavaScript-driven rendering), use the `imagex-picture.json.php` snippet:
148+
149+
```php
150+
<?php
151+
$options = [
152+
'image' => $image->toFile(),
153+
'ratio' => '16/9',
154+
'srcsetName' => 'my-srcset',
155+
// ... all other options work the same
156+
];
157+
158+
$json = snippet('imagex-picture.json', $options, true);
159+
echo $json;
160+
?>
161+
```
162+
163+
The JSON output contains structured data with `pictureAttributes`, `sources`, and `imgAttributes`. See the [JSON output example](/docs/examples/json-output.md) for more details.
164+
145165
### Snippet Options
146166
You can choose from many options to customize your images and pass them to the Imagex snippet. At first it might look heavy, but it's just very flexible and actually only `image` is required and everything else can be omitted, while Imagex is providing some sane defaults.
147167

docs/examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Every example is using the same `thumbs` config you can [see here](/docs/example
1010
4. [Custom Lazy Loading using `lazysizes`](/docs/examples/custom-lazy-loading.md)
1111
5. [Art-Directed Images](/docs/examples/art-directed-images.md)
1212
6. [Modern Configuration](/docs/examples/modern-config.md)
13+
7. [JSON Output](/docs/examples/json-output.md)

docs/examples/json-output.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

snippets/imagex-picture.json.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use TimNarr\Imagex;
4+
5+
$imagex = new Imagex([
6+
'critical' => $critical ?? false,
7+
'image' => $image,
8+
'imgAttributes' => $imgAttributes ?? ['shared' => [], 'eager' => [], 'lazy' => []],
9+
'pictureAttributes' => $pictureAttributes ?? ['shared' => [], 'eager' => [], 'lazy' => []],
10+
'ratio' => $ratio ?? 'intrinsic',
11+
'sourcesAttributes' => $sourcesAttributes ?? ['shared' => [], 'eager' => [], 'lazy' => []],
12+
'sourcesArtDirected' => $sourcesArtDirected ?? [],
13+
'srcsetName' => $srcsetName ?? 'default',
14+
'formatSizeHandling' => $formatSizeHandling ?? false,
15+
]);
16+
17+
$data = [
18+
'pictureAttributes' => $imagex->getPictureAttributes(),
19+
'sources' => $imagex->getPictureSources(),
20+
'imgAttributes' => $imagex->getImgAttributes(),
21+
];
22+
23+
return json_encode($data, JSON_PRETTY_PRINT);

0 commit comments

Comments
 (0)