Recover image dimensions when base metadata is incomplete (1x1 WebP)#2542
Draft
adamsilverstein wants to merge 2 commits into
Draft
Recover image dimensions when base metadata is incomplete (1x1 WebP)#2542adamsilverstein wants to merge 2 commits into
adamsilverstein wants to merge 2 commits into
Conversation
When wp_create_image_subsizes() cannot read an upload (an intermittent race where the file is read before it has finished being written), core returns attachment metadata without width/height/sizes. The Modern Image Formats filter then decorates and persists that record, leaving the attachment rendering at 1x1 pixels in the Media Library and on the front end while the file itself opens fine. Recover the original dimensions from the now-readable file before adding the sources property so the full-size image displays correctly, and bail without persisting a partial record when the file still cannot be read. Responsive sub-sizes still require regenerating the attachment. Refs WordPress#2468
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2542 +/- ##
==========================================
+ Coverage 69.87% 69.90% +0.03%
==========================================
Files 90 90
Lines 7797 7803 +6
==========================================
+ Hits 5448 5455 +7
+ Misses 2349 2348 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses the intermittent "1×1 px WebP" bug reported in #2468.
Root cause
The reporter's stored metadata for a broken attachment contained only the keys added by the performance plugins and none of the base metadata that core normally produces:
No
width,height,file, orsizes. That happens when core'swp_create_image_subsizes()bails early:wp_getimagesize()can fail intermittently at upload time (e.g. the file is read before it has finished being written to disk). Core then returns an empty array, thewp_generate_attachment_metadatafilters decorate it, and the incomplete record is persisted. Withwidth/heightmissing, core'swp_constrain_dimensions()clamps the dimensions tomax( 1, 0 ) = 1, which is why the image renders at 1×1 in the Media Library grid and in the front-end HTML, even though the file itself opens fine. This also explains the "random / works on re-upload" behavior: re-uploading creates a fresh attachment that usually processes cleanly.The change
By the time
webp_uploads_create_sources_property()runs, the file is readable again (thesourcesanddominant_colorkeys prove the file was read successfully a moment later). So when the base metadata is missing its dimensions, the filter now recoverswidth/heightfrom the file before attaching thesourcesproperty. If the file still can't be read, it bails without inventing dimensions or persisting a partial record.This restores correct display of the full-size image. Responsive sub-sizes (
sizes) are still absent in this failure case and require regenerating the attachment (wp media regenerate <id>).Why only the webp-uploads plugin
dominant-color-imagesis intentionally designed to extract the dominant color independently of base metadata (seetest-dominant-color.php, which asserts the color is added even when the incoming metadata isarray()). Thedominant_colorkey in the corrupted record is a witness, not a cause, so that plugin is left unchanged.Testing
test_it_should_recover_dimensions_when_base_metadata_is_incomplete— passes incomplete metadata and asserts the real dimensions are recovered andsourcesattached.test_it_should_not_decorate_metadata_when_file_is_missing— asserts no dimensions are invented and nosourcesadded when the file can't be read.Open questions / follow-ups
wp_getimagesize()failure; a more complete fix (retry, or regenerating sub-sizes) may warrant a core Trac ticket.Refs #2468