Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Tracked by #33447 ...
jasmussen opened this issue Feb 22, 2022 · 51 comments
Closed
Tracked by #33447 ...

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

jasmussen opened this issue Feb 22, 2022 · 51 comments
Assignees
Labels
[Block] Image Affects the Image Block [Block] Post Featured Image Affects the Post Featured Image Block [Block] Site Logo Affects the Site Logo Block [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Media Anything that impacts the experience of managing media Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Enhancement A suggestion for improvement.

Comments

@jasmussen
Copy link
Contributor

jasmussen commented Feb 22, 2022

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:

@mtias
Copy link
Member

mtias commented May 26, 2022

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.

@richtabor
Copy link
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. :)

@jasmussen

This comment was marked as outdated.

@isocialtish
Copy link

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

@MaggieCabrera
Copy link
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?

@jasmussen
Copy link
Contributor Author

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.

@MaggieCabrera
Copy link
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.

@jasmussen
Copy link
Contributor Author

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.

@MaggieCabrera
Copy link
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.

@scruffian
Copy link
Contributor

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

@mtias mtias added the Needs Design Needs design efforts. label Sep 10, 2022
@mtias
Copy link
Member

mtias commented Sep 10, 2022

Can we explore a design that combines the number presentation of the crop tools and a more visual symbol of what the rectangle looks like?

@jasmussen

This comment was marked as outdated.

@jasmussen jasmussen changed the title Image Block: Surface object-fit tool for custom image dimensions Image Block: Surface aspect-ratio tool for custom image dimensions Sep 13, 2022
@jasmussen jasmussen added Needs Dev Ready for, and needs developer efforts and removed Needs Design Needs design efforts. labels Sep 13, 2022
@cbirdsong
Copy link

cbirdsong commented May 10, 2023

This is great. The first thing users will ask is where is the Focal Point Picker? It needs to be included in the solution.

This was my immediate thought. People are going to use this on portraits and immediately get it cropping someone's head from the eyebrows down.

I would also raise this issue:

It is so wasteful to duplicate work across so many single-purpose blocks.

@jasmussen
Copy link
Contributor Author

This was my immediate thought. People are going to use this on portraits and immediately get it cropping someone's head from the eyebrows down.

Agreed. I wonder, can we approach this as a separate issue? The control that exists on the Cover block has a rather huge resting footprint:

Screenshot 2023-05-11 at 09 09 02

I like to think that we can potentially put that in an ItemGroup/Flyout combo to reduce its footprint, and in doing so port it to both Featured Image and Image blocks separately. What do you think?

@richtabor
Copy link
Member

@jasmussen I took the ideas and put them in today's Image block panel to theorize the simplest implementation for getting this into the 6.3 release. Here's the prototype.

What do you think of this approach, utilizing the existing Settings panel (where the block's image settings already are)?

aspect-ratio-3.mp4

Key concepts:

  • I like "Auto" better than "Original" for the initial "no aspect ratio" label; as it is used in the input fields too.
  • I've modified the select values to have additional context (i.e. "Square - 1:1"). It does seem a bit friendlier to me than just listing out numbers.
  • Uses standard select control, just like "Resolution", for easy implementation.
  • Applying an aspect ratio sets the height and width to "Auto".
  • Changing height or width (either Input or ResizableBox) will set aspect ratio back to "Auto".
  • I put "Scale" above the height/width controls, as they aren't actionable where scale is (not the same as post-featured-image thought).

Additional thoughts:

  • Do we need the scale control for cover/contain?
  • We should update the Post Featured Image block to use these same mechanics if possible.

@jasmussen
Copy link
Contributor Author

Prototype looks great, and agree with your takeaways, nice iteration. One question, though:

Changing height or width (either Input or ResizableBox) will set aspect ratio back to "Auto".

This is the tricky bit. Going back to the original issue, the challenge to solve is to allow you to create a pattern that looks like this:

... and then let you drop any odd image into that circular avatar dropzone to update it, and have it not explode. In other words, you need to be able to combine a custom width and height with the cover value, and if I'm not misunderstanding, then changing this back to "Auto" would break that ability.

@richtabor
Copy link
Member

Good point. We would technically need to separate image replacement from image resizing (even though the attributes do change when you replace an image). 🤔

@richtabor
Copy link
Member

So you're thinking if set to "cover" then the aspect ratio is maintained when replacing? Or we implement the whole syncing of height and width? #47963 (comment)

@jasmussen
Copy link
Contributor Author

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.

@richtabor richtabor added the Needs Design Feedback Needs general design feedback. label May 26, 2023
@richtabor
Copy link
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?

@jasmussen
Copy link
Contributor Author

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

That works.

@scruffian
Copy link
Contributor

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

@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Gutenberg Phase 2: Customization Jun 23, 2023
@github-project-automation github-project-automation bot moved this from ❓ Triage to ✅ Done in WordPress 6.3.x Editor Tasks Jun 23, 2023
@apeatling apeatling moved this from Needs dev to Done in 6.3 Design Jun 27, 2023
@bph bph added Needs Dev Note Requires a developer note for a major WordPress release cycle and removed Needs Design Feedback Needs general design feedback. Needs Dev Ready for, and needs developer efforts labels Jun 29, 2023
@rfischmann
Copy link

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

@AhmadRaza9
Copy link

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

@jasmussen
Copy link
Contributor Author

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
Labels
[Block] Image Affects the Image Block [Block] Post Featured Image Affects the Post Featured Image Block [Block] Site Logo Affects the Site Logo Block [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Media Anything that impacts the experience of managing media Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Enhancement A suggestion for improvement.
Projects
Status: Done
Status: Done
Development

No branches or pull requests