ux-image
Symfony UX Image: responsive img and picture tags with srcset, sizes and layout styles, transformed by a URL-based provider (Cloudflare, Cloudinary, imgix, Imagor, IPX, KeyCDN). Use for twig:ux:image, ux_image(), ux_picture(), ux_image_url(), UX_IMAGE_DSN, presets, LCP priority.
Install
npx skills add https://github.com/smnandre/symfony-ux-skills/tree/main/skills/ux-image
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install smnandre-symfony-ux-skills@llmmart
git clone https://github.com/smnandre/symfony-ux-skills.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole smnandre/symfony-ux-skills collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
UX Image
Render responsive images: srcset, sizes, loading, dimensions and a layout style, with every transformation delegated to a URL-based image provider. Without decoding or resizing anything itself, the package lets you switch providers and keep your templates.
Since Symfony UX 3.6+. The package is experimental: its API may still change.
Installation
composer require symfony/ux-image
composer require symfony/ux-twig-component # for the Twig functions and components
composer require symfony/ux-cloudflare-image # one provider bridge
Without TwigComponent, only the ImageUrlGenerator service is available (enough for an API or a worker).
# .env
UX_IMAGE_DSN=null://null # default: original URL, no srcset, keeps dimensions and style
# .env.prod
UX_IMAGE_DSN=cloudflare://cdn.example.com
| Scheme | Package | DSN example |
|---|---|---|
cloudflare |
symfony/ux-cloudflare-image |
cloudflare://cdn.example.com |
cloudinary |
symfony/ux-cloudinary-image |
cloudinary://my-cloud?origin=https://example.com |
imagor |
symfony/ux-imagor-image |
imagor://images.example.com?origin=https%3A%2F%2Fexample.com&sign_key=... |
imgix |
symfony/ux-imgix-image |
imgix://my-source.imgix.net |
ipx |
symfony/ux-ipx-image |
ipx://images.example.com |
keycdn |
symfony/ux-keycdn-image |
keycdn://myzone.kxcdn.com |
Rendering
<twig:ux:image src="/uploads/hero.jpg" alt="Hero" width="800" height="450" />
{# Above the fold / likely LCP: eager and high priority #}
<twig:ux:image src="/uploads/hero.jpg" alt="Hero" width="1200" height="600" priority />
{# One <source> per format, then the <img> fallback #}
<twig:ux:picture src="/uploads/hero.jpg" alt="Hero" width="800" height="450" />
src is the public path of the original, as your application serves it. Functions take the same options as a third argument (camelCase keys) and HTML attributes as a fourth:
{{ ux_image('/uploads/hero.jpg', 'Hero', {width: 800}, {class: 'rounded'}) }}
{{ ux_picture('/uploads/hero.jpg', 'Hero', {width: 800, height: 450}) }}
Single URL (Open Graph, email, CSS background, API):
<meta property="og:image" content="{{ absolute_url(ux_image_url('/uploads/hero.jpg', {width: 1200, height: 630})) }}">
use Symfony\UX\Image\ImageUrlGenerator;
$url = $this->imageUrlGenerator->generate('/uploads/hero.jpg', width: 1200, height: 630);
Props
| Prop | Default | Notes |
|---|---|---|
src, alt |
required | |
layout |
constrained |
fixed, constrained or full-width |
width, height |
null |
fixed/constrained require width; full-width requires height |
fit |
cover when both dimensions are set |
cover crops, contain letterboxes |
format |
null |
Pins one format: no negotiation, no <source> |
quality |
null |
1 to 100 |
priority |
false |
loading="eager" fetchpriority="high" instead of loading="lazy" |
object-fit (objectFit in functions) |
the fit value |
|
breakpoints |
resolutions option |
srcset widths for this image |
operations |
{} |
Provider-specific, keyed by provider name |
preset |
null |
Named transformation set |
Other attributes pass through to <img> and win over generated ones. A caller style merges with the layout style; a caller sizes replaces the generated one.
Layouts
| Layout | srcset widths |
sizes |
Style |
|---|---|---|---|
fixed |
width, 2 x width |
{width}px |
fixed width and height |
constrained |
width, 2 x width, ladder entries below 2 x width |
(min-width: {width}px) {width}px, 100vw |
max-width, aspect-ratio, width: 100% |
full-width |
whole ladder | 100vw |
width: 100%, aspect-ratio |
The generated sizes assumes the image spans the viewport below its width. In grids and sidebars, pass your own:
<twig:ux:image src="/uploads/card.jpg" alt="Card" width="400" height="300"
sizes="(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw" />
Configuration
# config/packages/ux_image.yaml
ux_image:
provider: '%env(resolve:UX_IMAGE_DSN)%' # resolve: is required
formats: ['avif', 'webp', 'jpeg'] # preference order, intersected with the provider's
resolutions: [6016, 5120, 4480, 3840, 3200, 2560, 2048, 1920, 1668, 1280, 1080, 960, 828, 750, 640]
quality: null # null leaves it to the provider
presets:
thumbnail: { width: 200, height: 200, quality: 70 }
og: { width: 1200, height: 630, format: jpeg }
vintage:
operations:
cloudflare: { saturation: 0.3, contrast: 1.2 }
<twig:ux:image src="/uploads/hero.jpg" alt="Hero" preset="thumbnail" />
{{ ux_image_url('/uploads/hero.jpg', {preset: 'og'}) }}
Presets hold only width, height, fit, format, quality and operations. Template values win per key; operations merge per provider and per operation.
<img> or <picture>
The provider never picks the element: the caller does. ux_image() always renders one <img>: providers that negotiate (Cloudflare, Cloudinary, imgix, IPX) still serve a modern format; otherwise the last surviving entry of formats is used. ux_picture() renders one <source> per format, which suits providers without negotiation (Imagor, KeyCDN) and caches that ignore Vary: Accept. Art direction (different crops per media query) is out of scope.
Provider Operations
<twig:ux:image src="/uploads/hero.jpg" alt="Hero" width="800"
:operations="{cloudflare: {sharpen: 1}, keycdn: {sharpen: 10}}" />
Only the active provider's entry is read. An unsupported operation, or a key naming a provider that is not installed, throws an exception. See references/providers.md for the operations, DSN options and signing of each provider.
Key Principles
Set priority on the LCP image. Everything is lazy by default; a lazy hero delays Largest Contentful Paint.
Version the path when replacing an original. Variants are cached by path and parameters: rather than reusing the name, use /uploads/hero.3f2a1c.jpg.
Develop with null://null. Templates, dimensions and layout styles are identical; only transformations are skipped.
Protect transformations in production. Each distinct URL is a new variant to generate and cache: enable URL signing where the provider supports it.
Keep formats and resolutions short. Every candidate is one more variant on the CDN.
Gotchas
InvalidArgumentException: missingwidthforfixed/constrained(orheightforfull-width), unknown option or preset, aformator operation the provider does not support, a DSN option it does not accept, or an emptyformatsintersection.- Bridge not found: each bridge registers only when its Composer package is installed.
%kernel.project_dir%in the DSN not resolved: use%env(resolve:UX_IMAGE_DSN)%.- Cloudflare URLs 404: enable Image Transformations on the zone.
- Cloudinary fetch refused: clear "Fetched URL" in Restricted media types, or sign URLs with
api_secret.
References
- Symfony UX package page (Markdown summary) ·
symfony/ux-imageon Packagist. - Try the responsive image demo.
- For rendering, presets, layouts and providers, see the Symfony UX Image documentation.
See Also
- twig-component: wrap
<twig:ux:image>in your own card or media components. - live-component: images re-render with the component like any Twig markup.
Files (symfony-ux-skills)
-
references
-
providers.md 6.9 KB
Loading…
-
-
SKILL.md 8.1 KB
--- name: ux-image description: 'Symfony UX Image: responsive img and picture tags with srcset, sizes and layout styles, transformed by a URL-based provider (Cloudflare, Cloudinary, imgix, Imagor, IPX, KeyCDN). Use for twig:ux:image, ux_image(), ux_picture(), ux_image_url(), UX_IMAGE_DSN, presets, LCP priority.' license: MIT metadata: author: Simon André url: https://smnandre.dev --- # UX Image Render responsive images: `srcset`, `sizes`, `loading`, dimensions and a layout `style`, with every transformation delegated to a URL-based image provider. Without decoding or resizing anything itself, the package lets you switch providers and keep your templates. Since Symfony UX 3.6+. The package is **experimental**: its API may still change. ## Installation ```bash composer require symfony/ux-image composer require symfony/ux-twig-component # for the Twig functions and components composer require symfony/ux-cloudflare-image # one provider bridge ``` Without TwigComponent, only the `ImageUrlGenerator` service is available (enough for an API or a worker). ```bash # .env UX_IMAGE_DSN=null://null # default: original URL, no srcset, keeps dimensions and style # .env.prod UX_IMAGE_DSN=cloudflare://cdn.example.com ``` | Scheme | Package | DSN example | |--------|---------|-------------| | `cloudflare` | `symfony/ux-cloudflare-image` | `cloudflare://cdn.example.com` | | `cloudinary` | `symfony/ux-cloudinary-image` | `cloudinary://my-cloud?origin=https://example.com` | | `imagor` | `symfony/ux-imagor-image` | `imagor://images.example.com?origin=https%3A%2F%2Fexample.com&sign_key=...` | | `imgix` | `symfony/ux-imgix-image` | `imgix://my-source.imgix.net` | | `ipx` | `symfony/ux-ipx-image` | `ipx://images.example.com` | | `keycdn` | `symfony/ux-keycdn-image` | `keycdn://myzone.kxcdn.com` | ## Rendering ```twig <twig:ux:image src="/uploads/hero.jpg" alt="Hero" width="800" height="450" /> {# Above the fold / likely LCP: eager and high priority #} <twig:ux:image src="/uploads/hero.jpg" alt="Hero" width="1200" height="600" priority /> {# One <source> per format, then the <img> fallback #} <twig:ux:picture src="/uploads/hero.jpg" alt="Hero" width="800" height="450" /> ``` `src` is the public path of the original, as your application serves it. Functions take the same options as a third argument (camelCase keys) and HTML attributes as a fourth: ```twig {{ ux_image('/uploads/hero.jpg', 'Hero', {width: 800}, {class: 'rounded'}) }} {{ ux_picture('/uploads/hero.jpg', 'Hero', {width: 800, height: 450}) }} ``` Single URL (Open Graph, email, CSS background, API): ```twig <meta property="og:image" content="{{ absolute_url(ux_image_url('/uploads/hero.jpg', {width: 1200, height: 630})) }}"> ``` ```php use Symfony\UX\Image\ImageUrlGenerator; $url = $this->imageUrlGenerator->generate('/uploads/hero.jpg', width: 1200, height: 630); ``` ## Props | Prop | Default | Notes | |------|---------|-------| | `src`, `alt` | required | | | `layout` | `constrained` | `fixed`, `constrained` or `full-width` | | `width`, `height` | `null` | `fixed`/`constrained` require `width`; `full-width` requires `height` | | `fit` | `cover` when both dimensions are set | `cover` crops, `contain` letterboxes | | `format` | `null` | Pins one format: no negotiation, no `<source>` | | `quality` | `null` | 1 to 100 | | `priority` | `false` | `loading="eager" fetchpriority="high"` instead of `loading="lazy"` | | `object-fit` (`objectFit` in functions) | the `fit` value | | | `breakpoints` | `resolutions` option | `srcset` widths for this image | | `operations` | `{}` | Provider-specific, keyed by provider name | | `preset` | `null` | Named transformation set | Other attributes pass through to `<img>` and win over generated ones. A caller `style` merges with the layout style; a caller `sizes` replaces the generated one. ## Layouts | Layout | `srcset` widths | `sizes` | Style | |--------|-----------------|---------|-------| | `fixed` | `width`, `2 x width` | `{width}px` | fixed width and height | | `constrained` | `width`, `2 x width`, ladder entries below `2 x width` | `(min-width: {width}px) {width}px, 100vw` | `max-width`, `aspect-ratio`, `width: 100%` | | `full-width` | whole ladder | `100vw` | `width: 100%`, `aspect-ratio` | The generated `sizes` assumes the image spans the viewport below its width. In grids and sidebars, pass your own: ```twig <twig:ux:image src="/uploads/card.jpg" alt="Card" width="400" height="300" sizes="(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw" /> ``` ## Configuration ```yaml # config/packages/ux_image.yaml ux_image: provider: '%env(resolve:UX_IMAGE_DSN)%' # resolve: is required formats: ['avif', 'webp', 'jpeg'] # preference order, intersected with the provider's resolutions: [6016, 5120, 4480, 3840, 3200, 2560, 2048, 1920, 1668, 1280, 1080, 960, 828, 750, 640] quality: null # null leaves it to the provider presets: thumbnail: { width: 200, height: 200, quality: 70 } og: { width: 1200, height: 630, format: jpeg } vintage: operations: cloudflare: { saturation: 0.3, contrast: 1.2 } ``` ```twig <twig:ux:image src="/uploads/hero.jpg" alt="Hero" preset="thumbnail" /> {{ ux_image_url('/uploads/hero.jpg', {preset: 'og'}) }} ``` Presets hold only `width`, `height`, `fit`, `format`, `quality` and `operations`. Template values win per key; `operations` merge per provider and per operation. ## `<img>` or `<picture>` The provider never picks the element: the caller does. `ux_image()` always renders one `<img>`: providers that negotiate (Cloudflare, Cloudinary, imgix, IPX) still serve a modern format; otherwise the last surviving entry of `formats` is used. `ux_picture()` renders one `<source>` per format, which suits providers without negotiation (Imagor, KeyCDN) and caches that ignore `Vary: Accept`. Art direction (different crops per media query) is out of scope. ## Provider Operations ```twig <twig:ux:image src="/uploads/hero.jpg" alt="Hero" width="800" :operations="{cloudflare: {sharpen: 1}, keycdn: {sharpen: 10}}" /> ``` Only the active provider's entry is read. An unsupported operation, or a key naming a provider that is not installed, throws an exception. See [references/providers.md](references/providers.md) for the operations, DSN options and signing of each provider. ## Key Principles **Set `priority` on the LCP image.** Everything is lazy by default; a lazy hero delays Largest Contentful Paint. **Version the path when replacing an original.** Variants are cached by path and parameters: rather than reusing the name, use `/uploads/hero.3f2a1c.jpg`. **Develop with `null://null`.** Templates, dimensions and layout styles are identical; only transformations are skipped. **Protect transformations in production.** Each distinct URL is a new variant to generate and cache: enable URL signing where the provider supports it. **Keep `formats` and `resolutions` short.** Every candidate is one more variant on the CDN. ## Gotchas - **`InvalidArgumentException`**: missing `width` for `fixed`/`constrained` (or `height` for `full-width`), unknown option or preset, a `format` or operation the provider does not support, a DSN option it does not accept, or an empty `formats` intersection. - **Bridge not found**: each bridge registers only when its Composer package is installed. - **`%kernel.project_dir%` in the DSN not resolved**: use `%env(resolve:UX_IMAGE_DSN)%`. - **Cloudflare URLs 404**: enable Image Transformations on the zone. - **Cloudinary fetch refused**: clear "Fetched URL" in Restricted media types, or sign URLs with `api_secret`. ## References - [Symfony UX package page](https://ux.symfony.com/image) ([Markdown summary](https://ux.symfony.com/image.md)) · [`symfony/ux-image` on Packagist](https://packagist.org/packages/symfony/ux-image). - Try the [responsive image demo](https://ux.symfony.com/image#demo). - For rendering, presets, layouts and providers, see the [Symfony UX Image documentation](https://symfony.com/bundles/ux-image). ## See Also - **twig-component**: wrap `<twig:ux:image>` in your own card or media components. - **live-component**: images re-render with the component like any Twig markup.
Reviews (0)
No reviews yet.
Comments
Sign in to join the conversation.
No comments yet. Start the conversation.