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

Definitively fix issues with images in AMP not visually matching non-AMP version #2036

Merged
merged 12 commits into from Apr 4, 2019

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Mar 29, 2019

Build for testing: amp.zip (v1.1-alpha-20190405T220345Z-9246809c)

This PR seeks to finally lay to rest the multitude of issues we've faced regarding amp-img not behaving like the corresponding img. A key piece to this is that the sizes attribute can now be removed from amp-img and the AMP runtime will (soon) compute it automatically; see ampproject/amphtml#19513.

Fixes issues including:

  • Featured image in Twenty Nineteen appears stretched in desktop.
  • Centered image is too side.
  • Align-wide images are not wide enough.
  • Align-full images are not wide enough.
  • Small inline image is not displayed tiny as required.
  • Image in Media/Text overflows right viewport (both normal and wide alignments).
  • Resized image in Media/Text is not the right dimensions.
  • Centered image in Classic block is not centered.
  • Inline image in Classic block appears on its own line.

See screenshots below to compare.

Fixes #1747. Fixes #1305.
See #1656, #1104, #1237, #1086.
Closes #1939.

The post content I've been using to test is as follows. I tried to consider every scenario for where an image can appear:

<!-- wp:image {"id":53,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53"/><figcaption>Bison: centered and not resized<br></figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>And here is an image without a caption and yet it is centered:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":53,"align":"center","width":247,"height":161} -->
<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53" width="247" height="161"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>And here is a centered image with a caption:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":53,"align":"center","width":252,"height":163} -->
<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53" width="252" height="163"/><figcaption>Resized and centered</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:image {"id":53,"align":"left","width":252,"height":163} -->
<div class="wp-block-image"><figure class="alignleft is-resized"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53" width="252" height="163"/><figcaption>Resized and left-floated</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:image {"id":53,"align":"right","width":252,"height":163} -->
<div class="wp-block-image"><figure class="alignright is-resized"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53" width="252" height="163"/><figcaption>Resized and right-floated</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! This text is in between two beasts! </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":53,"align":"wide"} -->
<figure class="wp-block-image alignwide"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53"/><figcaption>Wide</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":53,"align":"full"} -->
<figure class="wp-block-image alignfull"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53"/><figcaption>Full bleed<br></figcaption></figure>
<!-- /wp:image -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:paragraph -->
<p>Here is some text with an inline image: <img class="wp-image-53" style="width: 48px;" src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1.jpg" alt=""> See it is small?</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:media-text {"mediaPosition":"right","mediaId":53,"mediaType":"image","isStackedOnMobile":true} -->
<div class="wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…","fontSize":"large"} -->
<p class="has-large-font-size">Bison</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Image appears to the left. It is not resized. Stacking on mobile.</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->

<!-- wp:media-text {"align":"full","mediaPosition":"right","mediaId":53,"mediaType":"image","isStackedOnMobile":true} -->
<div class="wp-block-media-text alignfull has-media-on-the-right is-stacked-on-mobile"><figure class="wp-block-media-text__media"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…","fontSize":"large"} -->
<p class="has-large-font-size">Bison and full width</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Image appears to the left. It is not resized. Stacking on mobile.</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->

<!-- wp:media-text {"mediaId":53,"mediaType":"image","mediaWidth":19} -->
<div class="wp-block-media-text alignwide" style="grid-template-columns:19% auto"><figure class="wp-block-media-text__media"><img src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-1024x668.jpg" alt="" class="wp-image-53"/></figure><div class="wp-block-media-text__content"><!-- wp:paragraph {"placeholder":"Content…","fontSize":"large"} -->
<p class="has-large-font-size">Bison</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Image appears to the left. It <em>is</em> resized. Stacking off mobile.</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:media-text -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<p><img class="alignleft size-thumbnail wp-image-53" src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-150x150.jpg" alt="" width="150" height="150">This is a <strong>classic block</strong> that contains an image floated to the left. And there is one floated to the right here too:</p>
<p>[caption id="attachment_53" align="alignright" width="150"]<img class="size-thumbnail wp-image-53" src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-150x150.jpg" alt="" width="150" height="150"> Bison in field[/caption]</p>
<p>This is a classic block that contains an image floated to the left. This is a classic block that contains an image floated to the left. This is a classic block that contains an image floated to the left. This is a classic block that contains an image floated to the left. This is a classic block that contains an image floated to the left. This is a classic block that contains an image floated to the left. But the next image is centered:</p>
<p><img class="aligncenter wp-image-53 size-medium" src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-300x196.jpg" alt="" width="300" height="196"></p>
<p>And this centered image has a caption:</p>
<p>[caption id="attachment_53" align="aligncenter" width="300"]<img class="wp-image-53 size-medium" src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-300x196.jpg" alt="" width="300" height="196"> Bison in the field.[/caption]</p>
<p>Next line with tiny image inline:&nbsp;<img class="alignnone wp-image-53 " src="https://wordpressdev.lndo.site/content/uploads/2019/02/1200px-American_bison_k5680-1-150x150.jpg" alt="" width="16" height="16">. See how small it is?</p>
<p>Next line.</p>

Desktop

Non-AMP (control)✅ AMP with fix🚫 AMP without fix
desktop-non-ampdesktop-amp-with-fix

Mobile

Non-AMP (control)✅ AMP with fix🚫 AMP without fix
mobile-non-ampmobile-amp-with-fixmobile-amp-without-fix

Classic Templates

Please note that the Classic templates generally lack styles for blocks in general, so there are some issues that aren't related to images here.

🚫 Before changes✅ After changes
classic-amp-without-fixclassic-amp-with-fix

@westonruter
Copy link
Member Author

For some reason the alignfull isn't rendering properly in the Gutenberg demo post: https://2019-theme.amp-wp.org/welcome-to-the-gutenberg-editor/?amp

@westonruter
Copy link
Member Author

For some reason the alignfull isn't rendering properly in the Gutenberg demo post: https://2019-theme.amp-wp.org/welcome-to-the-gutenberg-editor/?amp

OK, that seems fixed now by 6677c3b.

@westonruter
Copy link
Member Author

As noted in ampproject/amphtml#21371 (comment) I added the Bison image test post to the environment:

* @todo: remove when this is resolved: https://github.com/ampproject/amphtml/issues/17053
* @since 1.0
*/
public static function remove_twentynineteen_thumbnail_image_sizes() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's great to be able to remove these workarounds!

@kienstra
Copy link
Contributor

kienstra commented Apr 3, 2019

Brief Testing

Hi @westonruter,
This looks good overall. There doesn't look to be distortion of images, and it's really nice seeing these workarounds removed.

It looks like sometimes large images are a different size in AMP and non-AMP. Though the biggest problem before was distortion, and that doesn't look to be an issue.

Steps to reproduce

  1. Activate Twenty Nineteen and checkout this PR's branch
  2. Create an Image block, without changing anything like the layout or caption
  3. Download a 1024 x 628 image
  4. In the Image block, click 'Upload' and select that image.
  5. Expected: it appears the same size in non-AMP and AMP
  6. Actual: it appears smaller in AMP

Non-AMP

non-amp-here

AMP

amp-narrower

@westonruter
Copy link
Member Author

@kienstra I think this is just an issue with your local environment. Note the image has the class name amp-wp-unknown-size. Compare that with the image at https://2019-theme.amp-wp.org/bison-image-test/

image

It appears as expected.

@westonruter
Copy link
Member Author

In other words, your image is failing because the plugin wasn't able to perform the HTTP request to determine the dimensions.

@kienstra
Copy link
Contributor

kienstra commented Apr 4, 2019

Thanks, @westonruter. That makes sense.

I'll review this PR.

@kienstra
Copy link
Contributor

kienstra commented Apr 4, 2019

Approved

Hi @westonruter,
Wow, this is the most thorough explanation with screenshots I've ever seen. Thanks for the great detail.

It's really nice that these workarounds aren't needed anymore.

(Stealing your great table formatting above)

Non-AMPAMP with this PR
non-ampamp

The only images that look different in AMP have amp-wp-unknown-size and amp-wp-unknown-width. That looks to be a similar issue to the one you commented about, possibly just the local environment.

As expected, there's no reference PR #17053 anymore in the plugin, like here.

/** Force the image into a box of fixed dimensions and use object-fit to scale. **/
object-fit: contain;
}
<?php echo file_get_contents( AMP__DIR__ . '/assets/css/amp-default.css' ); // phpcs:ignore WordPress.WP.AlternativeFunctions ?>
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea to echo amp-default.css here.

@westonruter westonruter merged commit 77b0b54 into develop Apr 4, 2019
@westonruter westonruter deleted the fix/image-stretching branch April 4, 2019 13:40
@westonruter
Copy link
Member Author

@kienstra I'm a bit concerned to see stretching here:

image

This should not be happening even when the sizes are unknown due to object-fit: contain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Signed the Google CLA
Projects
None yet
3 participants