Skip to content

Fix stretched images in rich-text output - #7904

Open
zuhairkhalid229 wants to merge 2 commits into
indico:masterfrom
zuhairkhalid229:fix/7889-image-aspect-ratio
Open

zuhairkhalid229 wants to merge 2 commits into
indico:masterfrom
zuhairkhalid229:fix/7889-image-aspect-ratio

Conversation

@zuhairkhalid229

Copy link
Copy Markdown

Fixes #7889.

The image in the issue is inserted with width="2048" height="1152". .editor-output img only sets max-width: 100%, so the width shrinks to fit while the height attribute keeps the image 1152px tall, which stretches it. Adding height: auto keeps the aspect ratio.

  • editor-output.scss: covers event descriptions (_details.html, used by lectures and meetings), conference descriptions, custom conference pages and minutes (_note.html puts editor-output on the same element as note-text, so the .note-text img rule in _meetings.scss needs no change, unlike what I said in the issue). TinyMCE uses editor-output as its body class, so the editor preview changes the same way.
  • _news.scss: .news-item .content img has the same max-width-only rule for TinyMCE news content.

Images with only a height attribute (no width) now render at their natural size, capped at the container width, instead of at that height. The image dialog writes both attributes, so this only affects hand-written HTML. img[width] { height: auto; } would avoid that if you prefer it.

Testing

  • Headless Chrome, a 2048×1152 image with those attributes in an 800px-wide .editor-output box: 800×1152 with the current rule, 800×450 with height: auto.
  • npx stylelint on the changed files and on indico/**/*.{scss,css}: no problems (run on my fork).

@zuhairkhalid229

Copy link
Copy Markdown
Author

The test-python-3.12 failure is indico/modules/rb/operations/bookings_test.py::test_ongoing_bookings_are_not_split[start_dt0-end_dt0] (KeyError: 'repeat_frequency'), unrelated to this CSS change. Its parameters use datetime.today(), which pytest evaluates at collection, and this job started at 23:58 Zurich time, so the run crossed midnight. The run for the previous commit of this PR (23:35) passed the same test. A re-run should pass.

@ThiefMaster

Copy link
Copy Markdown
Member

Indeed. I just fixed this flaky test in master.

@ThiefMaster

Copy link
Copy Markdown
Member

Headless Chrome

This makes me think there was no human involved... did you actually install Indico to test your change in a real browser?

@zuhairkhalid229

Copy link
Copy Markdown
Author

I've now run it in a real Indico, once on master and once on this branch: PostgreSQL and Redis, the built assets and indico run, with a lecture whose description has the markup from the issue (<img src="…" alt="" width="2048" height="1152">, a 2048×1152 image of a round moon), opened in Chrome at 1280px wide.

Before (master): the image is shown at 861×1152, so the moon is stretched into an oval.

before-master

After (this branch): 861×484, the image's 16:9 ratio.

after-fix

@NicolFedurcova NicolFedurcova left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I tested your solution, it does fix the image aspect ratio when width of the image exceeds the width of the container.

However as you noted, the height: auto ignores the user input height in the Insert/edit image dialog. So if user enters only height, or intentionally modifies the ratio of the image these changes do not affect the rendered image. Therefore the dialog is misleading and the height should not be modifiable.

The proposed fix with img[width] fixes the scenario when user inputs only the height (if the recalculated width is smaller than the container) however when both width and height are modified, the change in height is ignored. Also, if only height is provided as user input from the dialog, and the resized width exceeds the container, then the image is overflowing the container.

@zuhairkhalid229

Copy link
Copy Markdown
Author

Thanks for testing it, @NicolFedurcova. You're right: with height: auto the image always renders at the picture's own ratio, so a height entered in the dialog only counts through the width it implies, and a ratio changed on purpose is lost. The img[width] variant has the gaps you found as well.

I don't think CSS alone can do better here. To keep a custom ratio while shrinking the image to the container, the CSS would have to read the ratio from the width and height attributes. Browsers do map those attributes to aspect-ratio: auto <w> / <h>, but auto means the picture's own ratio wins once it has loaded, and the typed attr() that could read them explicitly only works in Chromium so far.

So your suggestion looks like the right one to me. TinyMCE 6 has an image_dimensions option; setting it to false removes the width and height fields from the image dialog. Images can still be resized by dragging the corner handles in the editor, and TinyMCE keeps the ratio while dragging (resize_img_proportional defaults to true), so the stored width and height match the picture and height: auto renders them as intended. The CSS part still matters on its own for existing descriptions whose images are wider than the container.

Would you like me to add image_dimensions: false to tinymce.js in this PR, or keep that as a separate change?

An image with width and height attributes (as the editor inserts) was
narrowed by max-width: 100% while its height attribute stayed, so it got
stretched. height: auto keeps its aspect ratio in .editor-output (event
descriptions, minutes, conference pages) and in news items.

Fixes indico#7889
@zuhairkhalid229
zuhairkhalid229 force-pushed the fix/7889-image-aspect-ratio branch from d2e0974 to 1f8c7d7 Compare November 28, 2026 20:23
@NicolFedurcova

Copy link
Copy Markdown
Contributor

I am not convinced that removing the width and height inputs from the UI dialog completely is the way to go. User is limited by the width of the TinyMCE editor when resizing image by dragging. If user wants to make an image fit the full width of the Lecture description, they need to drag the corner of the image outside of the TinyMCE editor (which is hard to discover). The best solution would therefore be to (only) disable the height input field in the Insert/edit image dialog, which semantically reflects the fact that height is adjusted automatically.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Image aspect ratio not respected in Lecture description

3 participants