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

Improving the calculation of image sizes attributes #760

Open
10 of 16 tasks
eclarke1 opened this issue Jun 20, 2023 · 3 comments
Open
10 of 16 tasks

Improving the calculation of image sizes attributes #760

eclarke1 opened this issue Jun 20, 2023 · 3 comments
Assignees
Labels
[Issue] Overview Provides an overview of a specific project [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) WP Core Work relates to inclusion in WP Core only

Comments

@eclarke1
Copy link

eclarke1 commented Jun 20, 2023

Overview

Originally introduced in WordPress 4.4, at the time WordPress had almost no way of knowing the intended layout of an image and therefore only came with a reasonable default value, intended to be modified by theme developers.

Since that time, enhancements to WordPress have been made, including the implementation of a declarative way for themes to control layout and alignments with theme.json, which provides the ability to more accurately predict the layout width of images before CSS is applied.

Paired with the addition of ”auto sizes” for lazy-loaded images to the HTML specification, we can now improve the sizes attribute authored by WordPress to provide much more accurate layout information.

Project Overview

Goals

  • Incorporate current content layout information, like layout.contentSize and layout.wideSize properties from theme.json in the default sizes attribute for images to improve accuracy.
  • Extend support for layout settings for defining alignment sizes to classic themes so that the optimizations can be made available to themes that do not support theme.json.
  • When layout information from the above is not available, attempt to use the available global Content Width value to constrain image sizes values.
  • Make the sizes attribute aware of an image’s layout in relation to any ancestor blocks, like columns, group block alignment, etc.
  • Implement the auto-sizes spec for any images that are being lazy-loaded.

Nice to have

  • Account for available spacing information like padding, margin, and block gaps when calculating the sizes attribute.

Non-goals

  • The sizes attribute needs to be 100% accurate at all viewport sizes for a theme.
  • Implementing client side linting tools like respimagelint to determine display.
  • Requiring the use front end metrics collected by an API like Optimization Detective.
  • Modifying the available image URLs included in the srcset attribute to provide more granular sources for clients to request at different viewport widths.

Roadmap

This project is broken into the following two epics, with milestones for each epic describing major objectives that could be published as part of a plugin release version. This roadmap will be updated over time.

Improving the sizes calculation

This epic is broken into logical milestones to validate the use of theme.json data in the API before proposing needed enhancements to that API, and then rolling out enhancements to classic themes in order to support the largest number of sites.

Implement auto-sizes for lazy loaded images

This epic outlines the phases of development planned to implement the auto-sizes spec in WordPress.

@eclarke1 eclarke1 added [Issue] Overview Provides an overview of a specific project [Focus] Images labels Jun 20, 2023
@joemcgill joemcgill added the WP Core Work relates to inclusion in WP Core only label Sep 11, 2023
@joemcgill joemcgill self-assigned this Sep 21, 2023
@joemcgill joemcgill added the [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) label Apr 29, 2024
@joemcgill
Copy link
Member

I've updated the description on this issue to better outline the scope of this project and a high-level roadmap of tasks to be worked on related to this effort, which I plan to convert into standalone issues.

As we begin work on improving the default sizes calculation, I think it makes sense to include that work and the auto-sizes work in the same plugin, rather than having two different plugins that are so closely related.

@sstopfer sstopfer added [Focus] Images WP Core Work relates to inclusion in WP Core only and removed [Focus] Images WP Core Work relates to inclusion in WP Core only labels May 26, 2024
@joemcgill
Copy link
Member

Update

I've updated the description to start to better prioritize the existing issues for improving the sizes calculation into what I would consider required for an initial feature release, and those that could be shipped in follow up releases.

Currently, we have all work related to the improved sizes calculation epic staged in the https://github.com/WordPress/performance/tree/feature/more-accurate-sizes-attribute feature branch, which can be merged to trunk once all of the initial feature release issues (and any newly emerging issues) are addressed.

@mukeshpanchal27
Copy link
Member

mukeshpanchal27 commented Jul 31, 2024

For classic themes

When we implement the accurate sizes for classic theme we can use POC - https://github.com/Automattic/jetpack/blob/99baf6dfba29881377085a9ed375f611bfe60ac1/projects/packages/image-cdn/src/class-image-cdn.php#L1039-L1061

add_filter( 'wp_calculate_image_sizes', 'filter_sizes', 1, 2 ); // Early so themes can still easily filter.
function filter_sizes( $sizes, $size ) {
	if ( wp_is_block_theme() ) {
		return $sizes;
	}

	if ( ! doing_filter( 'the_content' ) ) {
		return $sizes;
	}

	$content_width = ( isset( $GLOBALS['content_width'] ) && is_numeric( $GLOBALS['content_width'] ) )
		? $GLOBALS['content_width']
		: false;

	if ( ! $content_width ) {
		$content_width = 1000; // Jetpack use these number ,we can update it.
	}

	if ( ( is_array( $size ) && $size[0] < $content_width ) ) {
		return $sizes;
	}

	return sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Issue] Overview Provides an overview of a specific project [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) WP Core Work relates to inclusion in WP Core only
Projects
Status: In Progress 🚧
Status: In Progress
Development

No branches or pull requests

4 participants