Skip to content

Image Block: Surface aspect-ratio tool for custom image dimensions #38990

Closed

Description

@jasmussen

Many pattern designs rely on a specific dimension of an image in order to provide a compelling example. For example this circular avatar pattern:

circular avatar in a quote pattern

At the moment, if you mean to update this person with a photo of your own, unless you replace it with a new image of the same dimensions and crop, the pattern will be broken. In order to solve this, the pattern should be able to retain its dimensions and aspect-ratio, so even if you drag a landscape photo into the pattern, it will be correctly masked to fit.

In the user interface, we can surface such preset aspect ratios in a flyout menu. This keeps the width/height controls, as the Featured Image has:

ar plus width

One issue that exists with the featured image block in trunk is that if you apply both a width and a height, the aspect ratio essentially becomes non-functional. This shouldn't happen, so a few things we can do, both for Image, and for Featured Image as a followup.

We can, when you apply an aspect ratio, the width value is removed, set visually to "Auto" and disabled. You'd have to choose "original" aspect ratio to enable them again.

An image with an aspect ratio can work together with an explicit width, however, it just can't work with both width and height. So what we could do instead is to clear the aspect ratio back to "Original" if you fill out both width and height values. I.e.:

  • Selecting an aspect ratio clears height.
  • Adding back a height clears the aspect ratio.

We can potentially pair this with a snackbar that notes why either is being cleared.


This ticket was updated May 10.

Previous version

In the user interface, we can surface such preset aspect ratios in a dropdown. Shown here, 6 common presets:

AR Dropdown

The dropdown affords a rich visual shape representation of the aspect ratio numbers. When an aspect ratio is applied, the Height input field says "Auto", to indicate that its value is dynamically dependant on the width.

If you then proceed to add a custom value to the height field after all, that gives you a custom aspect ratio:

AR Dropdown, Custom

This is simple to translate to CSS as the aspect ratio of a 540x320 image as shown in the example can simply be stated using aspect-ratio: 540 / 320;. See also this codepen for how that can work responsively.

To represent aspect ratios, especially as custom aspect ratios are possible, it would be useful if the shape icon was dynamically drawn based on each aspect ratio.

This ticket was updated Sep. 13.

See initial proposal

When you set square image dimensions on a rectangular image, the image gets skewed (note, there's a bug in the editor view at the moment). The skewed image is not that useful, and rarely what you want.

Since we know the original aspect ratio, we can surface a scaling tool when a non-natural aspect ratio is shown, letting you choose the scaling type so that the image gets masked instead of skewed:

Masking i4

The tool would simply apply object-fit: cover; to the image, and work the same as how the Featured Image block does, letting you choose the scaling mode in a segmented control:

Screenshot 2022-02-22 at 15 44 55

The ability to set the scaling mode would be very useful for patterns that perhaps include circular photos which when replaced with your own non-square images could become pill-shaped. Site Logo could also benefit from the maintaining the original aspect ratio.

In order for masked image to scale responsively according to a new aspect ratio, the aspect-ratio property can be leveraged as is shown in this codepen.

See also:

Activity

  1. mtias commented on May 26, 2022

    @mtias
    Member

    Instead of prioritizing explicit width / height, I'd want to explore using the aspect ratio tool (similar to the hard-cropping one) used. We can apply it to the cover block as well. We can still support resizing while maintaining aspect ratio.

    We should also do this in conjunction with #41142 to support the creation of pattern with placeholders much better.

  2. richtabor commented on May 27, 2022

    @richtabor
    Member

    Instead of prioritizing explicit width / height, I'd want to explore using the aspect ratio tool

    I agree.

    It's a bit rudimentary, but I've experimented with aspect-ratio tied to image sizes — where selecting an aspect ratio calculates the proper height value from the width (and the object-fit crops it properly). Then when you resize, the height/width ratio is maintained.

    CleanShot.2022-05-27.at.10.39.47.mp4

    This is just an example, not the UI I think we should go with this implementation. :)

  3. jasmussen commented on Aug 22, 2022

    @jasmussen
    Author
  4. isocialtish commented on Aug 28, 2022

    @isocialtish

    I was just speaking with a customer who was requesting this feature specifically in the Post Featured Image block.

  5. MaggieCabrera commented on Sep 8, 2022

    @MaggieCabrera
    Contributor

    Would the Aspect ratio options actually change the dimension values or would they change the aspect-ratio CSS value?

    As shortcuts to setting dimensions as well as applying object-fit: cover;, a single unified control is much simpler.

    How do you decide which object fit to use in which case? And what happens when the user provides its own dimensions, do we still see the object fit controls?

  6. jasmussen commented on Sep 8, 2022

    @jasmussen
    ContributorAuthor

    Would the Aspect ratio options actually change the dimension values or would they change the aspect-ratio CSS value?

    I'd love for it to set aspect-ratio. It wouldn't work in all browsers, but as a progressive enhancement it would in new ones, and it would gracefully fall back.

    How do you decide which object fit to use in which case? And what happens when the user provides its own dimensions, do we still see the object fit controls?

    Stretch isn't that useful, and something you could accomplish on your own if you wanted to, by supplying off dimensions. Contain could have use cases, but I also think we could start without it, and then add a choice between the type of object-fit when we find that necessary.

  7. MaggieCabrera commented on Sep 8, 2022

    @MaggieCabrera
    Contributor

    Would the Aspect ratio options actually change the dimension values or would they change the aspect-ratio CSS value?

    I'd love for it to set aspect-ratio. It wouldn't work in all browsers, but as a progressive enhancement it would in new ones, and it would gracefully fall back.

    But if dimensions are given, aspect ratio won't apply at all. And I think all images do have inline width and height values, I'd have to test it on the block. Still, controlling both dimensions and aspect ratio would be contradictory.

  8. jasmussen commented on Sep 8, 2022

    @jasmussen
    ContributorAuthor

    Just to step back for a moment, the main problem to solve with the aspect ratio presets is to enable you to drag and drop your big landscape photo into this pattern, and have the perfect circle and dimensions of the original pattern stick and be unchanged, just with your new updated photo:
    Screenshot 2022-09-08 at 16 46 41

    When you pick an aspect ratio, I suppose the 2nd value becomes determined by the first one, so we could potentially gray out one of the width/height input fields if need be.

    I also made this codepen which dives into some of the responsiveness.

  9. MaggieCabrera commented on Sep 8, 2022

    @MaggieCabrera
    Contributor

    When you pick an aspect ratio, I suppose the 2nd value becomes determined by the first one, so we could potentially gray out one of the width/height input fields if need be.

    That sounds reasonable! and I agree that aspect-ratio is preferable, I was worried about confusing interactions with it.

    Thanks for providing the extra context for this.

  10. scruffian commented on Sep 9, 2022

    @scruffian
    Contributor

    When we get round to this we should add the same tools to the Post Featured Image block.

  11. 59 remaining items

  12. jasmussen commented on May 25, 2023

    @jasmussen
    ContributorAuthor

    Could we do it so an image replacement (drop one image on top of another) never changes its dimensions? It seems like when I design the pattern and set the image to 100x100+cover, I should be able to trust that's and remains.

    If the main question is "when do we show the cover/contain" control, can we do that whenever dimensions are entered that aren't the intrinsic ones from the image?

    Worst case, we could have a "Custom" aspect ratio, which is just width/height. Like, if you set your image to 400x200, we apply aspect-ratio: 400/200;, and it works.

  13. richtabor commented on May 30, 2023

    @richtabor
    Member

    Worst case, we could have a "Custom" aspect ratio, which is just width/height. Like, if you set your image to 400x200, we apply aspect-ratio: 400/200;, and it works.

    I'm ok with this.

    If the main question is "when do we show the cover/contain" control, can we do that whenever dimensions are entered that aren't the intrinsic ones from the image?

    What about if aspect ratio is not Auto/Original, then the cover/contain control is available?

  14. jasmussen commented on May 31, 2023

    @jasmussen
    ContributorAuthor

    What about if aspect ratio is not Auto/Original, then the cover/contain control is available?

    That works.

  15. scruffian commented on Jun 23, 2023

    @scruffian
    Contributor

    Now that #51545 is merged I think we can close this.

  16. moved this from 🏗 In progress to ✅ Done in Gutenberg Phase 2: Customizationon Jun 23, 2023
  17. moved this from Needs dev to Done in 6.3 Designon Jun 27, 2023
  18. added
    Needs Dev NoteRequires a developer note for a major WordPress release cycle
    and removed
    Needs DevReady for, and needs developer efforts
    on Jun 29, 2023
  19. rfischmann commented on Nov 9, 2023

    @rfischmann

    Why were the 25%/50%/75%/100%/Reset buttons removed since WordPress 6.3? They were so useful… 😓

  20. AhmadRaza9 commented on Aug 20, 2024

    @AhmadRaza9

    Is there any way to modify Gutenberg Image Block "Aspect Ratio" Settings, I want to add my custom aspect ratio

  21. jasmussen commented on Aug 27, 2024

    @jasmussen
    ContributorAuthor

    Here's a mockup showing how we might reintroduce those 25/50/75/100% shortcuts.

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

Metadata

Metadata

Assignees

Labels

Needs Dev NoteRequires a developer note for a major WordPress release cycle[Block] ImageAffects the Image Block[Block] Post Featured ImageAffects the Post Featured Image Block[Block] Site LogoAffects the Site Logo Block[Feature] ExtensibilityThe ability to extend blocks or the editing experience[Feature] MediaAnything that impacts the experience of managing media[Type] EnhancementA suggestion for improvement.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions