Skip to content

[Image] Add Image component - #3765

Open
seb-jean wants to merge 1 commit into
symfony:3.xfrom
seb-jean:feat/image-component
Open

[Image] Add Image component#3765
seb-jean wants to merge 1 commit into
symfony:3.xfrom
seb-jean:feat/image-component

Conversation

@seb-jean

Copy link
Copy Markdown
Contributor
Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? yes
Issues Fix #3188 / #3549
License MIT

This PR adds a new symfony/ux-image package providing Twig components for rendering <img>, <picture> and <source> HTML elements.

Components

  • <twig:ux:img> : renders an <img> element (runtime renderer, void element)
  • <twig:ux:picture> : renders a <picture> wrapper (template-based, accepts children)
  • <twig:ux:source> : renders a <source> element (runtime renderer, void element)

Twig functions

  • ux_img() : programmatic alternative to the component
  • ux_picture() : programmatic alternative with sources array

Supported attributes

All standard HTML attributes: src, alt, srcset, sizes, width, height, loading, decoding, fetchpriority, crossorigin, referrerpolicy, ismap, usemap. Extra attributes (class, id, data-*, …) are passed through.

srcset and sizes accept both strings and PHP arrays for readability.

Validation

The components validate attributes against the HTML specification at render time:

  • alt is always required (empty string allowed for decorative images)
  • src or srcset must be provided
  • srcset must not mix width (w) and density (x) descriptors
  • srcset must not contain duplicate descriptors
  • sizes requires all srcset candidates to use w descriptors
  • sizes="auto" (or "auto, <fallback>") requires loading="lazy"
  • Enum attributes (loading, decoding, fetchpriority, crossorigin, referrerpolicy) are validated
  • <source> does not accept src

Example

{# Basic image #}
<twig:ux:img src="/photo.jpg" alt="A photo" width="800" height="600" loading="lazy" />
{# Responsive with srcset #}
<twig:ux:img
    src="/photo-800.jpg"
    alt="Hero"
    :srcset="['/photo-480.jpg 480w', '/photo-800.jpg 800w', '/photo-1200.jpg 1200w']"
    :sizes="['(max-width: 600px) 480px', '800px']"
/>
{# Format selection: AVIF → WebP → JPEG #}
<twig:ux:picture>
    <twig:ux:source srcset="/photo.avif" type="image/avif" />
    <twig:ux:source srcset="/photo.webp" type="image/webp" />
    <twig:ux:img src="/photo.jpg" alt="A photo" width="800" height="600" />
</twig:ux:picture>

@carsonbot carsonbot added Documentation Improvements or additions to documentation Feature New Feature Status: Needs Review Needs to be reviewed labels Aug 11, 2026
@seb-jean
seb-jean force-pushed the feat/image-component branch from e9afd28 to 7748f93 Compare August 11, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Feature New Feature Status: Needs Review Needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants