Skip to content

Conversation

@westonruter
Copy link
Contributor

In testing a site using the AMP plugin in Standard/Transitional mode and in AMP Stories, I noticed a couple issues where Photon could better support AMP.

Proposed Changes

Support photonification of amp-img like img

I noticed that an <amp-img> put directly into post_content does not get processed by Photon. This isn't particularly surprising since it would be looking for <img> not <amp-img> (or <amp-anim>). To fix this, the regex in \Jetpack_Photon::parse_images_from_html() just needs to be updated like so:

- (?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag><img[^>]*?\s+?src=["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*</a>)?
+ (?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag><(?:img|amp-img|amp-anim)[^>]*?\s+?src=["|\'](?P<img_url>[^\s]+?)["|\'].*?>){1}(?:\s*</a>)?

Given post_content (e.g. a Custom HTML block) containing:

<amp-img src="https://example.com/wp-content/uploads/2019/06/downasaur-150x150.jpg" alt="" class="wp-image-9451" width="100" height=100></amp-img>

Before, with no change because Photon didn't see it:

<amp-img src="https://example.com/wp-content/uploads/2019/06/downasaur-150x150.jpg" alt="" class="wp-image-9451" width="100" height="100"></amp-img>

After, with image being replaced with Photon CDN as expected:

<amp-img src="https://i2.wp.com/example.com/wp-content/uploads/2019/06/downasaur.jpg?resize=100%2C100&amp;ssl=1" alt="" class="wp-image-9451" width="100" height="100"></amp-img>

Remove unnecessary data-recalc-dims=1

Additionally I noticed that Photon was adding the data-recalc-dims=1 attribute to images, but this is irrelevant in AMP since the devicepx JS is not loaded. So I'm not omitting this in AMP.

Prevent constraining to content_width in Stories

Originally reported in ampproject/amp-wp#2595.

The AMP Stories experience (amp_story plugin) intends to constrain the height of images to be 1440px, given the vertical resolution of mobile devices. This turned out to fail to work as expected when Photon supplied, since it constrains based on the theme's content_width.

For example, when setting the background image for a story page when a theme has a 640 content width, the generated markup with Photon is:

<amp-img layout="fill" src="https://i2.wp.com/example.com/wp-content/uploads/2019/06/downasaur.jpg?w=640&amp;ssl=1" class="amp-wp-6ed12b8"></amp-img>

The w=640 is the problem here. What is actually desired is h=1440.

In order to accomplish this, this PR adds a jetpack_photon_post_image_args filter which runs when on a singular amp_story post to override the args to be appropriate for the format.

In order to achieve this, additional context params needed to be passed to jetpack_photon_post_image_args in order to basically re-factor the following logic using a fixed height of 1440px instead of the content_width:

jetpack/class.photon.php

Lines 422 to 458 in 596bdd6

// If width is available, constrain to $content_width
if ( false !== $width && false === strpos( $width, '%' ) && is_numeric( $content_width ) ) {
if ( $width > $content_width && false !== $height && false === strpos( $height, '%' ) ) {
$height = round( ( $content_width * $height ) / $width );
$width = $content_width;
} elseif ( $width > $content_width ) {
$width = $content_width;
}
}
// Set a width if none is found and $content_width is available
// If width is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing
if ( false === $width && is_numeric( $content_width ) ) {
$width = (int) $content_width;
if ( false !== $height )
$transform = 'fit';
}
// Detect if image source is for a custom-cropped thumbnail and prevent further URL manipulation.
if ( ! $fullsize_url && preg_match_all( '#-e[a-z0-9]+(-\d+x\d+)?\.(' . implode('|', self::$extensions ) . '){1}$#i', basename( $src ), $filename ) )
$fullsize_url = true;
// Build URL, first maybe removing WP's resized string so we pass the original image to Photon
if ( ! $fullsize_url ) {
$src = self::strip_image_dimensions_maybe( $src );
}
// Build array of Photon args and expose to filter before passing to Photon URL function
$args = array();
if ( false !== $width && false !== $height && false === strpos( $width, '%' ) && false === strpos( $height, '%' ) )
$args[ $transform ] = $width . ',' . $height;
elseif ( false !== $width )
$args['w'] = $width;
elseif ( false !== $height )
$args['h'] = $height;

When this change is paired with support for photonizing <amp-img> in post_content (which the amp_story post type does), the result is a user can successfully upload new large images and have them appear with the expected 1440px height as opposed to the full size or a content_width-constrained size.

Testing instructions:

  1. Upload a large image to the media library (do this first before the following).
  2. Activate photon module
  3. Install and activate AMP v1.2 and enable the Stories experience.
  4. Upload another large image.
  5. Create a new story and select the first uploaded image as a page background, then add another page and use the second image as its background.
  6. Confirm that both story pages have images that include h=1440.

Proposed changelog entry for your changes:

  • Improve AMP support in Photon, including handling of <amp-img> and <amp-anim> in post_content and ensuring proper height-constrained images are used in AMP Stories. Adds additional context to jetpack_photon_post_image_args filter, and remove AMP-unnecessary data-recalc-dims attribute.

@jetpackbot
Copy link
Collaborator

jetpackbot commented Jun 22, 2019

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: August 6, 2019.
Scheduled code freeze: July 30, 2019

Generated by 🚫 dangerJS against b7875e1

@jeherve jeherve added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] Photon aka "Image CDN". Feature developed in the Image CDN package and shipped in multiple plugins [Status] Needs Review This PR is ready for review. AMP [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Jun 24, 2019
@jeherve
Copy link
Member

jeherve commented Jun 24, 2019

That would be a nice improvement!

Do you think you could add some tests in tests/php/test_functions.photon.php, to make sure we don't break this in the future with other changes?

@westonruter
Copy link
Contributor Author

@jeherve Thanks for the review. I believe I've addressed your feedback in what I just pushed.

@jeherve jeherve added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jul 3, 2019
@jeherve jeherve added this to the 7.6 milestone Jul 3, 2019
@jeherve jeherve self-requested a review July 18, 2019 16:30
@jeherve jeherve dismissed their stale review July 18, 2019 16:31

Changes have been addressed

@jeherve jeherve force-pushed the add/photon-amp-compat branch from 15771bb to cf053c2 Compare July 29, 2019 12:07
@jeherve jeherve force-pushed the add/photon-amp-compat branch from cf053c2 to b7875e1 Compare July 29, 2019 12:10
@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello westonruter! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer, review, and approve D30922-code before merging this PR. Thank you!

@jeherve jeherve removed the [Status] Needs Review This PR is ready for review. label Jul 29, 2019
@jeherve jeherve added the [Status] Ready to Merge Go ahead, you can push that green button! label Jul 29, 2019
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This works well for me. This should be good to merge once we fix the Unit Test breakage caused by https://core.trac.wordpress.org/changeset/45687/

@zinigor zinigor merged commit 2c8fe44 into Automattic:master Jul 29, 2019
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Jul 29, 2019
jeherve added a commit that referenced this pull request Jul 30, 2019
jeherve added a commit that referenced this pull request Jul 30, 2019
* Add initial changelog / testing list changes for 7.6

* Update stable tag to 7.5.3

* changelog: add #12957

* Changelog: add #12932

* Changelog: add #12867

* Changelog: add #12823

* changelog: add #12969

* changelog: add #13012

* changelog: add #12974

* Changelog: add #13059

* Changelog: add #13079

* Changelog: add #12924

* changelog: add #12954

* Changelog: add #12959

* Changelog: add #12977

* Changelog: add #12830

* Changelog: add #12926

* Changelog: add #12958

* Changelog: add #12999

* Changelog: add #13077

* Changelog: add #13083

* Changelog: add #13087

* Changelog: add #13110

* Changelog: add #13116

* Changelog: add #13117

* Changelog: add #12821

* Changelog: add #13120

* changelog: add #13139

* Changelog: add #13143

* Changelog: add #13147

* Testing list: add section about sync
jeherve added a commit that referenced this pull request Jul 30, 2019
* Add initial changelog / testing list changes for 7.6

* Update stable tag to 7.5.3

* changelog: add #12957

* Changelog: add #12932

* Changelog: add #12867

* Changelog: add #12823

* changelog: add #12969

* changelog: add #13012

* changelog: add #12974

* Changelog: add #13059

* Changelog: add #13079

* Changelog: add #12924

* changelog: add #12954

* Changelog: add #12959

* Changelog: add #12977

* Changelog: add #12830

* Changelog: add #12926

* Changelog: add #12958

* Changelog: add #12999

* Changelog: add #13077

* Changelog: add #13083

* Changelog: add #13087

* Changelog: add #13110

* Changelog: add #13116

* Changelog: add #13117

* Changelog: add #12821

* Changelog: add #13120

* changelog: add #13139

* Changelog: add #13143

* Changelog: add #13147

* Testing list: add section about sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AMP [Feature] Photon aka "Image CDN". Feature developed in the Image CDN package and shipped in multiple plugins Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants