#33641 closed task (blessed) (fixed)
Add support for responsive images using srcset and sizes
Reported by: | joemcgill | Owned by: | kirasong |
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Media | Keywords: | has-patch |
Focuses: | Cc: |
Description (last modified by )
When image markup is generated in WordPress, we should automatically include srcset
and sizes
attributes when possible in order to provide basic responsive image support.
To provide this, members of the Responsive Images Community Group created a feature plugin, with development on GitHub.
The plugin uses the default image sizes and available custom image sizes of the same aspect ratio when generating markup. It does not create additional sizes.
This is a placeholder ticket for initial patches, to become a tracking ticket if/when the plugin is approved for merge.
Attachments (8)
Change History (37)
This ticket was mentioned in Slack in #feature-respimg by joemcgill. View the logs.
9 years ago
#2
@
9 years ago
- Description modified (diff)
- Owner set to DH-Shredder
- Status changed from new to assigned
- Summary changed from Add support for srcset and sizes. to Add support for responsive images using srcset and sizes
This ticket was mentioned in Slack in #core by joemcgill. View the logs.
9 years ago
This ticket was mentioned in Slack in #feature-respimg by joemcgill. View the logs.
9 years ago
#5
@
9 years ago
- Milestone changed from Awaiting Review to 4.4
- Type changed from enhancement to task (blessed)
This ticket was mentioned in Slack in #feature-respimg by joemcgill. View the logs.
9 years ago
This ticket was mentioned in Slack in #feature-respimg by joemcgill. View the logs.
9 years ago
This ticket was mentioned in Slack in #core by wonderboymusic. View the logs.
9 years ago
#9
@
9 years ago
- Keywords has-patch added
This is a first pass at a patch for adding responsive image support to core, via srcset
and sizes
, to core.
## Code changes
This patch adds the following new functions to media.php:
wp_get_attachment_image_srcset_array
- Returns an array of image candidate string data used to build asrcset
value for an attachment given anattachement_id
andsize
.wp_get_attachment_image_srcset
- Returns thesrcset
value for an attachment given anattachement_id
andsize
.wp_get_attachment_image_sized
- Returns thesizes
value for an attachment given anattachement_id
andsize
and optional arguments used to alter its output.wp_make_content_images_responsive
- A display filter for addingsrcset
andsizes
to images embedded in content.wp_img_add_srcset_and_sizes
- A utility function used bywp_make_content_images_responsive
to addsrcset
andsizes
to a singleimg
element.
This patch also modifies the following existing core functions:
- Modify
wp_get_attachment_image
so the HTML returned for an image includessrcset
andsizes
. - Modify
get_media_embedded_in_content
by addingimg
to the list of accepted tags that can be matched in content. This is used inwp_make_content_images_responsive
to find all of the images embedded in content before passing them off towp_img_add_srcset_and_sizes
.
## Unit tests
This adds 12 new unit tests to tests/media.php
and a new factory method to WP_UnitTest_Factory_For_Attachment
named create_upload_object
which creates an attachment and metadata from a file, as discussed in #34075. Several existing unit tests were updated to make use of this new factory method.
## Performance info
Based on our internal tests, the display filter is adding ~1ms per image being filtered. https://github.com/ResponsiveImagesCG/wp-tevko-responsive-images/pull/207#issuecomment-145506195
Running the wp_make_content_images_responsive
display filter adds 2 queries to page loads total. By passing an array of all the attachment ids to _prime_post_caches
, we avoid needing to query for post data associated with each individual image being transformed.
This ticket was mentioned in Slack in #core by joemcgill. View the logs.
9 years ago
This ticket was mentioned in Slack in #feature-respimg by joemcgill. View the logs.
9 years ago
#12
@
9 years ago
I noticed that the test image didn't get included in the patch for some reason. test-image-large.png needs to be in the /tests/phpunit/data/images/
folder before running the tests added by this patch.
#13
@
9 years ago
Added 33641.2.patch which includes some docs and coding standards adjustments.
Notable stuff:
- Adding backticks in inline comments is moot because they aren't parsed
- Added a changelog entry for the
media_embedded_in_content_allowed_types
hook - Updated the version on the
wp_image_sizes_args
hook (safe to assume 2.4.0 is the version of the plugin) - In a couple of functions, I renamed the
$id
parameter to$attachment_id
for self-documentation purposes - Some spacing and syntax in various places.
Overall, this is extremely well-documented. Nice work.
#15
follow-up:
↓ 16
@
9 years ago
Hi Shapeshifter,
The patch number reflects the Trac ticket number associated with the patch, which has no relation to the version number of the plugin this code is coming from.
Thanks!
#16
in reply to:
↑ 15
@
9 years ago
Replying to joemcgill:
Hi Shapeshifter,
The patch number reflects the Trac ticket number associated with the patch, which has no relation to the version number of the plugin this code is coming from.
Thanks!
I learned something...Thank You...Can't wait for this to happen!
#18
@
9 years ago
Caught an outdated inline comment that slipped through. 33641.3.patch removes that comment.
#20
follow-up:
↓ 21
@
9 years ago
If a filter filters out img_width, a PHP notice is thrown since we're checking for a non-variable.
#21
in reply to:
↑ 20
;
follow-up:
↓ 22
@
9 years ago
Replying to kraftbj:
Good catch. Maybe we could simplify and just do a if ( ! empty( $image_width ) )
check there.
#22
in reply to:
↑ 21
;
follow-up:
↓ 23
@
9 years ago
Replying to joemcgill:
Good catch. Maybe we could simplify and just do a
if ( ! empty( $image_width ) )
check there.
Need more coffee. That's the way to go. Adding patch that if ( empty ( $img_width ) )
and adjusts the inline doc above to match var names.
#23
in reply to:
↑ 22
@
9 years ago
Replying to kraftbj:
Perfect. And thanks for catching the mistake in my sloppy comment code.
#27
@
9 years ago
In two tests for wp_calculate_image_srcset()
we were using a random size name "yoav". This gives the impression that we are testing what happens if an invalid size name is used, but we are not. The test helper function _get_image_size_array_from_name()
returns the default size array in this case so we are testing the code with a normal size array.
In 33641.6.diff I removed this size name from the tests and I added a new test that actually tests an invalid size name.
I thought this was the best place to add the patch even though the ticket closed. Let me know if I should open a new ticket for it.
First patch for #33641