Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#50543 closed defect (bug) (fixed)

Media: wp_image_src_get_dimensions() may throw warning or return wrong values

Reported by: azaozz's profile azaozz Owned by: azaozz's profile azaozz
Milestone: 5.5 Priority: normal
Severity: normal Version: 5.5
Component: Media Keywords: has-patch needs-testing
Focuses: Cc:

Description

Happens when the attachment ID (typically hard-coded in the post content) used to get the image meta has changed for some reason, for example when the site was exported and imported.

Attachments (3)

50543.diff (1.9 KB) - added by azaozz 5 years ago.
50543.1.diff (1.9 KB) - added by azaozz 5 years ago.
50543.2.diff (5.4 KB) - added by azaozz 5 years ago.

Download all attachments as: .zip

Change History (13)

@azaozz
5 years ago

#1 @azaozz
5 years ago

  • Keywords has-patch needs-testing needs-unit-tests added

In 50543.diff:

  • Check if the $image_meta is actually the meta data for an image.
  • Check if the relative uploads path plus the file name is at the end of the source URL before returning width and height from $image_meta.

Would also be great to add a unit test or two, perhaps after beta is out :)

@azaozz
5 years ago

#2 @azaozz
5 years ago

In 50543.1.diff: also account for edge case when an uploaded image is smaller than the smallest sub-size ($image_meta['sizes'] is empty). We still need the width and height if the file and path matches.

#3 follow-up: @Otto42
5 years ago

Patch works to eliminate the problem on my w.org sandbox.

#4 in reply to: ↑ 3 @azaozz
5 years ago

Replying to Otto42:

Thanks for testing! :)

Seems the same functionality (check if the attachment ID matches the image) will be needed at least at one more place, in the edit image end point. Will update the patch and commit.

@azaozz
5 years ago

#5 @azaozz
5 years ago

  • Keywords needs-unit-tests removed

In 50543:

  • Introduce wp_image_file_matches_image_meta() and wp_image_file_matches_image_meta filter as this will be needed in few places.
  • Add few tests for the new function.

#6 @azaozz
5 years ago

  • Owner set to azaozz
  • Resolution set to fixed
  • Status changed from new to closed

In 48329:

Media:

  • Introduce wp_image_file_matches_image_meta() utility function to check whether the image meta (retrieved by attachment ID) matches an image path or URI. A mismatch may happen in some cases, for example after the posts have been exported from one website and imported in another.
  • Add unit tests for the new function.
  • Improve wp_image_src_get_dimensions() a bit and use the new function to prevent these edge cases.

Fixes #50543.

#7 follow-up: @Otto42
5 years ago

Thanks! I will monitor the warnings and let you know if anything crops up.

#8 in reply to: ↑ 7 @azaozz
5 years ago

Replying to Otto42:
Great, thanks!

#9 @SergeyBiryukov
5 years ago

In 48331:

Coding Standards: Fix WPCS issues in tests/media.php.

See #50543.

#10 @azaozz
5 years ago

In 48430:

Media: Tiny logic fix in wp_image_file_matches_image_meta() after [48329]. No need to look in sizes if the full size image path/URL matches.

See #50543.

Note: See TracTickets for help on using tickets.