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

Improve story page background media with image srcset, reduced image size, and a11y text #3006

Merged
merged 15 commits into from
Aug 20, 2019

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Aug 11, 2019

Fixes #2835. Revisits #2332, #2509.

  • Changes amp_story_page image size to 1280px high instead of 1440px high.
  • Instead of adding an amp-img for the amp-story-page background image element, this PR adds an img similar to how the core/image block does, including the important wp-image-{attachment_id} class. This ensures that wp_make_content_images_responsive() is able to supply a srcset for the image (whereas currently there is none), potentially allowing reduced bandwidth when more appropriate image sizes are available; this allows for plugins like Jetpack to supply dynamically-generated image sizes. The image sanitizer converts the img to an amp-img. The inclusion of srcset also means this logic can be replaced with just const mediaUrl = media.url:

const mediaUrl = has( media, [ 'sizes', MAX_IMAGE_SIZE_SLUG, 'url' ] ) ? media.sizes[ MAX_IMAGE_SIZE_SLUG ].url : media.url;

  • Improves accessibility by adding an alt attribute to the background image, and an aria-label to the background video.
  • Uses the object-position attribute instead of the object-position style. This reduces the amount of CSS included style[amp-custom] and avoids the opaque amp-wp-... class name.

Still needing to be done:

  • Add block deprecation logic.
  • Add 1800px-tall image size for desktop stories?
  • Simplify logic in \AMP_Story_Media::add_new_max_image_size()? See c330ce7.

Rendered Output Before

...
<amp-story-grid-layer template="fill">
	<amp-img
		src="https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-2208x1280.jpg"
		layout="fill"
		class="amp-wp-896316c"
	></amp-img>
</amp-story-grid-layer>
...

Rendered Output After

...
<amp-story-grid-layer template="fill">
	<amp-img
		src="https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2.jpg"
		layout="fill"
		alt="American Bison"
		class="wp-image-2911 amp-wp-enforced-sizes"
		object-position="85% 50%"
		srcset="https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-1963x1280.jpg 1963w, https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-300x196.jpg 300w, https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-1024x668.jpg 1024w, https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-768x501.jpg 768w, https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-1200x783.jpg 1200w, https://wordpressdev.lndo.site/content/uploads/2019/08/American_bison_k5680-1-2-1400x913.jpg 1400w"
	></amp-img>
</amp-story-grid-layer>
...

@westonruter westonruter added this to the v1.2.1 milestone Aug 11, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Aug 11, 2019
@westonruter westonruter changed the title Add srcset and alt attributes to amp-story-page background images Improve story page background image and video with srcset, reduced size, and alt Aug 11, 2019
@westonruter
Copy link
Member Author

I'm somewhat out of my depth for the deprecated handling.

/cc @swissspidy

@@ -134,13 +135,15 @@ class PageEdit extends Component {
mediaType = media.type;
}

const mediaUrl = has( media, [ 'sizes', MAX_IMAGE_SIZE_SLUG, 'url' ] ) ? media.sizes[ MAX_IMAGE_SIZE_SLUG ].url : media.url;
Copy link
Member Author

Choose a reason for hiding this comment

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

@kienstra This appears no longer to be necessary when the srcset is present.

Originated in 0bd76bc

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that makes sense.

@swissspidy
Copy link
Collaborator

@miina Mind taking a look at adding the block deprecation for the changed img tag?

@miina
Copy link
Contributor

miina commented Aug 12, 2019

Mind taking a look at adding the block deprecation for the changed img tag?

Sure, checking now.

@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: no Has not signed the Google CLA and removed cla: yes Signed the Google CLA labels Aug 12, 2019
@miina
Copy link
Contributor

miina commented Aug 12, 2019

@googlebot I consent.

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes Signed the Google CLA and removed cla: no Has not signed the Google CLA labels Aug 12, 2019
@miina
Copy link
Contributor

miina commented Aug 12, 2019

Added the deprecation,

  • Templates need an update as well,

will check that, too, to be able to test the deprecation better.

@westonruter
Copy link
Member Author

  • Eliminate now-unnecessary unsetting of $image_sizes['full'] in \AMP_Story_Media::add_new_max_image_size()? See c330ce7.

@kienstra Thoughts on how much of this is still relevant now?

@kienstra
Copy link
Contributor

kienstra commented Aug 13, 2019

Hi @westonruter,

Eliminate now-unnecessary unsetting of $image_sizes['full'] in \AMP_Story_Media::add_new_max_image_size()? See c330ce7.

@kienstra Thoughts on how much of this is still relevant now?

Good question.

I think the elseif block that unsets the full image may still be relevant, but the first if block probably isn't.

1. First if block

if ( isset( $_POST['action'] ) && ( 'query-attachments' === $_POST['action'] || 'upload-attachment' === $_POST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$image_sizes[ self::MAX_IMAGE_SIZE_SLUG ] = $full_size_name;

^ This probably isn't needed anymore. It had 2 purposes:

a) On clicking 'Background Media' > 'Select Media' and uploading an image, it adds self:: MAX_IMAGE_SIZE_SLUG to the available image sizes

b) In the Image block, on clicking 'Media Library,' it adds the self:: MAX_IMAGE_SIZE_SLUG to the available image sizes in the Media Library

But removing that if block doesn't seem to have an effect. Like you mentioned, in this PR the Background Media doesn't look at media.sizes, it uses the media.url.

2. elseif block

} elseif ( get_post_type() && AMP_Story_Post_Type::POST_TYPE_SLUG === get_post_type() ) {
$image_sizes[ self::MAX_IMAGE_SIZE_SLUG ] = $full_size_name;
unset( $image_sizes['full'] );
}

Deleting this conditional did have an effect, and I'm not sure if it'd be good to delete it now.

To recap what it does, in the Image block's 'Image Size' <select>, it removes the 'Full Size', and adds 'Story Max Size':

filter-removes

Without that code above, a user can select a full size image. And it looks like the srcset doesn't reduce the image size enough.

Though the user would have to select the full size image, large is still the default.

For example:

  1. Checkout this PR's branch and delete this line:
    add_filter( 'image_size_names_choose', [ __CLASS__, 'add_new_max_image_size' ] );
  2. Create an Image block
  3. Click 'Media Library'
  4. Upload a 4000 x 4000 image
  5. In the 'Image Size' <select>, choose 'Full Size'
  6. Publish the story, and go to the front-end
  7. Expected: the srcset produces a currentSrc at least a little smaller than the full 4000 x 4000 size
  8. Actual: the currentSrc is 4000 x 4000

full-size-image

This seems to be the case even with smaller image sizes, like 2000 x 2000.

With this PR's branch as is, without deleting that line, it's a little better:

story-here

@westonruter
Copy link
Member Author

@kienstra Thanks a lot for the thorough response. I did some testing as well and it turns out that when doing device emulation (simulating Pixel 2, for example) the srcset causes the full size to be reduced to the dimensions of the story page automatically. The srcset is not properly calculated when looking at a non-landscape story on a desktop viewport. So this being the case, I think it's fine actually to leave the full size. Done in 9e96dfe.

@westonruter
Copy link
Member Author

I think we can defer adding a new image size for desktop for a future release.

@westonruter westonruter marked this pull request as ready for review August 19, 2019 22:03
@westonruter westonruter requested a review from swissspidy August 19, 2019 22:42
@westonruter
Copy link
Member Author

@miina Could you resolve the conflicts with the templates?

@westonruter
Copy link
Member Author

Humm, E2E failures?

@miina
Copy link
Contributor

miina commented Aug 20, 2019

Hmph, there's a test that doesn't pass reliably, will restart the tests, should look into improving this test again. Thought that it got fixed already but apparently not.

@miina
Copy link
Contributor

miina commented Aug 20, 2019

Or actually, this time it looks like it's another test. Looking into this now.

@miina
Copy link
Contributor

miina commented Aug 20, 2019

A different test is failing every time. Hard to debug as well since locally not failing.

It might be related to the issue in this version of Puppeteer that page.click doesn't seem to work reliably and instead page.evaluate should be used for consistent results, e.g.:

        await page.evaluate( ( selector ) => {
		document.querySelector( selector ).click();
	}, btnSelector );

The upstream helpers use the page.click, perhaps we should try replacing those and then try. @swissspidy Any other ideas?

@swissspidy
Copy link
Collaborator

Looking at the tests now (tests/e2e/specs/stories-editor/cta.test.js failed), but what I can see is that there are block validation errors. Mind checking that, @miina?

@westonruter westonruter changed the title Improve story page background image and video with srcset, reduced size, and alt Improve story page background media with image srcset, reduced image size, and a11y text Aug 20, 2019
@swissspidy
Copy link
Collaborator

Skipped that flaky test for now; can be investigated separately.

@swissspidy swissspidy merged commit f5cbc6f into develop Aug 20, 2019
@swissspidy swissspidy deleted the add/amp-story-page-background-image-srcset branch August 20, 2019 19:11
@miina
Copy link
Contributor

miina commented Aug 20, 2019

Will look into that CTA test.

@westonruter
Copy link
Member Author

Opened Jetpack PR to reduce Photon-sized images from 1440px to 1280px: Automattic/jetpack#13278

westonruter added a commit that referenced this pull request Aug 22, 2019
* tag '1.2.1': (434 commits)
  Bump 1.2.1
  Adjust block inserter style. (#3075)
  Update dependency eslint-plugin-jest to v22.15.2
  Fix flaky CTA test (#3057)
  Add more resizing handles (#3023)
  Bump version to 1.2.1-RC1
  Improve story page background media with image srcset, reduced image size, and a11y text (#3006)
  Wait until content loaded before calculating font size. (#3052)
  Harden logic for normalizing image metadata before adding story images (#3049)
  Update dependency uuid to v3.3.3 (#3046)
  Inline color support (#3033)
  Update dependency webpack-cli to v3.3.7 (#3040)
  Tiny prop-types fix
  Update dependency babel-jest to v24.9.0 (#3037)
  Update e2e test setup (#3031)
  Include amp-script among dynamic_element_selectors in tree shaking
  Another try to fix tests.
  Ensure selecting the first page before removing the block.
  Move setting input selection to the end to helpers.
  Fix editor test.
  ...
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
Development

Successfully merging this pull request may close these issues.

Re-evaluate maximum image size in stories
5 participants