Skip to content

Reduce maximum image size used as background image based on container width - #2258

Open
nani-samireddy wants to merge 19 commits into
WordPress:trunkfrom
nani-samireddy:enhancement/reduce-maximum-image-size-used-as-background-image
Open

Reduce maximum image size used as background image based on container width#2258
nani-samireddy wants to merge 19 commits into
WordPress:trunkfrom
nani-samireddy:enhancement/reduce-maximum-image-size-used-as-background-image

Conversation

@nani-samireddy

@nani-samireddy nani-samireddy commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

Summary

Fixes #2216

Relevant technical choices

This change exposes background image attachment IDs and original URLs from block attributes (core/group and core/cover) as data attributes on the rendered HTML element, and uses those attributes in Image Prioritizer to pick and serve a more appropriate-sized background image. It enables downsizing background images based on collected URL Metrics (preferring desktop metrics to avoid serving overly small images to desktop visitors).

Use of AI Tools

@codecov

codecov Bot commented Nov 8, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.15663% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.70%. Comparing base (79e7ac7) to head (cf0d687).

Files with missing lines Patch % Lines
...ns/auto-sizes/includes/improve-calculate-sizes.php 89.77% 9 Missing ⚠️
...rioritizer-background-image-styled-tag-visitor.php 83.72% 7 Missing ⚠️
plugins/image-prioritizer/helper.php 91.30% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2258      +/-   ##
==========================================
+ Coverage   70.35%   70.70%   +0.35%     
==========================================
  Files          91       91              
  Lines        7867     8023     +156     
==========================================
+ Hits         5535     5673     +138     
- Misses       2332     2350      +18     
Flag Coverage Δ
multisite 70.70% <89.15%> (+0.35%) ⬆️
single 35.37% <43.37%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nani-samireddy
nani-samireddy marked this pull request as ready for review November 8, 2025 06:48
@github-actions

github-actions Bot commented Nov 8, 2025

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: nani-samireddy <nanisamireddy@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Image Prioritizer Issues for the Image Prioritizer plugin (dependent on Optimization Detective) [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) and removed [Plugin] Image Prioritizer Issues for the Image Prioritizer plugin (dependent on Optimization Detective) labels Jan 15, 2026
@westonruter westonruter added this to the auto-sizes n.e.x.t milestone Jan 15, 2026
@westonruter westonruter changed the title Enhancement: reduce maximum image size used as background image based on container width Enhanced Responsive Images: Reduce maximum image size used as background image based on container width Jan 15, 2026
@westonruter westonruter added the [Plugin] Image Prioritizer Issues for the Image Prioritizer plugin (dependent on Optimization Detective) label Jan 15, 2026
@westonruter westonruter changed the title Enhanced Responsive Images: Reduce maximum image size used as background image based on container width Reduce maximum image size used as background image based on container width Jan 15, 2026
@westonruter
westonruter requested a review from Copilot January 15, 2026 00:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR implements background image size optimization for core/group and core/cover blocks by exposing attachment IDs as data attributes and using URL Metrics to serve appropriately-sized images based on container dimensions.

Changes:

  • Added auto_sizes_add_background_image_data_attributes function to extract and expose background image attachment IDs and URLs as data attributes on rendered HTML elements
  • Implemented reduce_background_image_size method in Image Prioritizer to downsize background images based on desktop viewport metrics
  • Registered new filter hooks for core/group and core/cover blocks at priority 5

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php Added method to reduce background image sizes using attachment IDs from data attributes and URL Metrics
plugins/auto-sizes/includes/improve-calculate-sizes.php Added function to extract background image data from block attributes and add as data attributes to HTML
plugins/auto-sizes/hooks.php Registered filter hooks for core/group and core/cover blocks to add background image data attributes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread plugins/auto-sizes/includes/improve-calculate-sizes.php Outdated
Comment thread plugins/auto-sizes/includes/improve-calculate-sizes.php Outdated
Comment thread plugins/auto-sizes/includes/improve-calculate-sizes.php Outdated
westonruter and others added 3 commits February 26, 2026 13:38
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
westonruter and others added 4 commits February 26, 2026 13:50
…lify return logic

Consolidate multiple return statements into a single return at the end of auto_sizes_add_background_image_data_attributes(), keeping only the initial guard clause.

Co-authored-by: gemini-cli <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Consolidate duplicated logic for obtaining maximum element width into a protected method in the parent Image_Prioritizer_Tag_Visitor class. The new method returns null if the element is not found, providing a more robust check in subclasses.

Co-authored-by: gemini-cli <176961590+gemini-code-assist[bot]@users.noreply.github.com>
… enhancement/reduce-maximum-image-size-used-as-background-image

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for the long delay in following up on this.

Comment on lines +265 to +266
// Try to get the attachment ID from the data attribute (populated via filter from block attributes).
$attachment_id = $processor->get_attribute( 'data-bg-attachment-id' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a plugin dependency issue here. Currently this requires that the auto-sizes plugin be active in order for this attribute to be added. This dependency shouldn't exist. This attribute should be added in the Image Prioritizer plugin for its own use. If the Image Prioritizer plugin isn't active, then the auto-sizes plugin should be able to scale down the image all on its own because it has access to template layout information on its own. The Image Prioritizer implementation is a parallel one which will benefit sites without auto-sizes active, or sites which are using a classic theme in which case the layout information is not available.

while ( $processor->next_tag() ) {
$style = $processor->get_attribute( 'style' );
if ( is_string( $style ) && str_contains( $style, 'background-image:' ) && str_contains( $style, $image_url ) ) {
$processor->set_attribute( 'data-bg-attachment-id', (string) $attachment_id );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As noted below, this attribute is being added here exclusively for the use of another plugin: Image Prioritizer. This dependency needs to be removed. Instead, this needs to be determining the size of the block as informed by the layout it has access to based on the block structure on a block template.

In other words, I believe all of the logic here in auto_sizes_filter_render_block_context() needs to be re-factored into auto_sizes_filter_uses_context(), auto_sizes_filter_render_block_context(), and auto_sizes_filter_image_tag(), for example, to follow the pattern of how other blocks are currently handled.

Comment thread plugins/auto-sizes/hooks.php Outdated
Comment on lines +34 to +35
add_filter( 'render_block_core/group', 'auto_sizes_add_background_image_data_attributes', 5, 2 );
add_filter( 'render_block_core/cover', 'auto_sizes_add_background_image_data_attributes', 5, 2 );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Taking auto_sizes_filter_image_tag() for inspiration, something like:

Suggested change
add_filter( 'render_block_core/group', 'auto_sizes_add_background_image_data_attributes', 5, 2 );
add_filter( 'render_block_core/cover', 'auto_sizes_add_background_image_data_attributes', 5, 2 );
add_filter( 'render_block_core/group', 'auto_sizes_filter_background_image_style', 5, 2 );
add_filter( 'render_block_core/cover', 'auto_sizes_filter_background_image_style', 5, 2 );

A new auto_sizes_filter_background_image_style() function will be needed modeled after auto_sizes_filter_image_tag().

@nani-samireddy
nani-samireddy requested review from westonruter and a lite review from Copilot August 6, 2026 18:55
…ving attribute processing and metadata retrieval
@nani-samireddy

Copy link
Copy Markdown
Contributor Author

@westonruter I made the updates to the PR.
Could you please take a look?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Note

This error may be related to your runner configuration. You can now configure runners for Copilot code review separately from Copilot cloud agent by creating a copilot-code-review.yml file with your setup steps. Read the docs for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) [Plugin] Image Prioritizer Issues for the Image Prioritizer plugin (dependent on Optimization Detective) [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce maximum image size used as background image based on container width

3 participants