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

Fix extractor that forgot duplicate images #1314

Merged
merged 5 commits into from Aug 11, 2018
Merged

Fix extractor that forgot duplicate images #1314

merged 5 commits into from Aug 11, 2018

Conversation

lukas9393
Copy link
Contributor

@lukas9393 lukas9393 commented Aug 3, 2018

What happened:
In this case are two dimensionless images in one post with the same URI. One image of them includes additional the Host. The image with the URL is returned by the extractor with dimensions. Comparing to the other image - the image using the URI is returning without dimension.

The Post:

<img class="alignnone size-medium wp-image-5" src="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg" alt="" width="300" />

<img class="alignnone size-medium wp-image-5" src="/wp-content/uploads/2018/06/IMG_0183-300x300.jpg" alt="" width="300" />

The Content:

<p><amp-img class="alignnone size-medium wp-image-5 amp-wp-unknown-size amp-wp-unknown-height amp-wp-enforced-sizes" src="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg" alt="" width="300" srcset="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg 300w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-150x150.jpg 150w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-100x100.jpg 100w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" height="400" layout="intrinsic"></amp-img></p>
<p><amp-img class="alignnone size-medium wp-image-5 amp-wp-enforced-sizes" src="/wp-content/uploads/2018/06/IMG_0183-300x300.jpg" alt="" width="300" srcset="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg 300w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-150x150.jpg 150w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-100x100.jpg 100w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183.jpg 640w" sizes="(max-width: 300px) 100vw, 300px"></amp-img></p>

The height is missing in the second image.

What you expected to happen:

<p><amp-img class="alignnone size-medium wp-image-5 amp-wp-unknown-size amp-wp-unknown-height amp-wp-enforced-sizes" src="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg" alt="" width="300" srcset="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg 300w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-150x150.jpg 150w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-100x100.jpg 100w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" height="400" layout="intrinsic"></amp-img></p>
<p><amp-img class="alignnone size-medium wp-image-5 amp-wp-unknown-size amp-wp-unknown-height amp-wp-enforced-sizes" src="/wp-content/uploads/2018/06/IMG_0183-300x300.jpg" alt="" width="300" srcset="http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-300x300.jpg 300w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-150x150.jpg 150w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183-100x100.jpg 100w, http://localhost:8080/wp-content/uploads/2018/06/IMG_0183.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" height="400" layout="intrinsic"></amp-img></p>

What's happening:

The Extractor normalizes the URLs and stores the original url under its normalized urls. As a result, one of the original url is lost. The reason for this is that the normalized url should point to two original url. That's not possible.
Finally, the extractor iterates over the extracted dimensions. The extracted dimensions are just a subset of the original url. Only one original url is used.

Some possible resolutions:

The extractor stores the original urls as keys. The normalized url are stored as a value. That means we have no losses.
In the end the extractor iterates over the original urls. For each original url he gets the normalized url and asks for its dimension. This dimension is saved for the original urls.

Anything else we need to know?:

AMP: 1.0-beta1
Wordpress: 4.9.7
Container: Wordpress:latest
Theme: Twenty Seventeen 1.6
OS: MacOS 10.13.5

hellofromtonya
hellofromtonya previously approved these changes Aug 3, 2018
@westonruter westonruter self-assigned this Aug 3, 2018
Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. Would you please merge the latest changes in from develop since another PR (#793) also just modified this file. (Update: I just merged the updates.)

With the file then updated, you please add a unit test to verify the fix.

@westonruter
Copy link
Member

It seems the changes in this PR now create a unit test failure:

1) AMP_Image_Dimension_Extractor_Extract_Test::test__should_return_original_urls
Undefined index: data:image/gif;base64,R0lGODl...
/tmp/wordpress/src/wp-content/plugins/amp/includes/utils/class-amp-image-dimension-extractor.php:49
/tmp/wordpress/src/wp-content/plugins/amp/tests/test-amp-image-dimension-extractor.php:85

https://travis-ci.org/Automattic/amp-wp/jobs/411801773#L253

@hellofromtonya hellofromtonya dismissed their stale review August 3, 2018 21:53

Breaking change upon develop merge.

$normalized_urls[] = $normalized_url;
} else {
// This is not a URL we can extract dimensions from, so default to false.
$url_map[ $original_url ] = $original_url;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this line being removed? It was recently added in #793.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the old version the line is unnecessary. The code after this line never uses url_map to get an url that cannot be normalized. Therefore, this line is unnecessary and can be removed.

In my version, it is also unnecessary and causes an error.

@westonruter westonruter changed the title fix extractor that forgot duplicate images Fix extractor that forgot duplicate images Aug 11, 2018
@westonruter westonruter merged commit 8c4f8ea into ampproject:develop Aug 11, 2018
@westonruter westonruter added this to the v1.0 milestone Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants