Skip to content

Conversation

@felixarntz
Copy link
Member

@felixarntz felixarntz commented Sep 2, 2022

This is a work in progress to bring awareness of the image size name to WP_Image_Editor, and then pass the value to the image_editor_output_format filter, so that filter callbacks can make a contextual decision based on the image size. This will then be used to specify the output format for image/jpeg to be conditionally image/webp, depending on whether this is configured for the given image size.

Trac ticket: https://core.trac.wordpress.org/ticket/56526

Relevant decisions

  • Size name awareness in WP_Image_Editor has been implemented in a fully backward compatible way which also works with any custom sub class implementations.
    • A distinct class property is used, to not conflict with the existing $size property.
    • A setter and getter function can be used to modify the size name.
    • All existing base class methods retain their current signature (changing this would cause breakage with custom implementations).
  • Modifies the wp_default_image_output_mapping() filter callback to now set the image/jpeg to image/webp mapping only if the given size supports it.
  • Test coverage for the additions to the wp_default_image_output_mapping() function is included.

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Overall this looks really good, left some doc block alignment suggestions. I'm going to review and test this locally as well

@felixarntz
Copy link
Member Author

@adamsilverstein In 7df09fd, I've added the relevant logic to control WebP output per size, which we can later iterate on further based on the add_image_size() parameter. Please have a look.

Left to do here is expanding test coverage to account for the new logic there.

@felixarntz felixarntz marked this pull request as ready for review September 7, 2022 17:34
@felixarntz
Copy link
Member Author

This should be ready for a comprehensive review (and hopefully commit) now.

if ( $is_image ) {
/** This filter is documented in wp-includes/class-wp-image-editor.php */
$output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type );
$output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type, '' );
Copy link
Member

Choose a reason for hiding this comment

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

We should call out in the dev note that there is no size when core calls wp_unique_filename because this is applied only to the upload image (not the sub-sizes). might be confusing that it is called this way.

Copy link
Member Author

Choose a reason for hiding this comment

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

@adamsilverstein Good point, the additional parameter for this definitely needs to be explained in the dev note. FWIW, it's not only in wp_unique_filename that this is empty, it's always empty when no particular image size is being handled. For example even within WP_Image_Editor usage, the parameter will be an empty string for the original image, or the full image, or any custom operation (e.g. cropping the site icon). Only interactions that relate to a specific registered image size will result in the parameter being not empty.

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Excellent work!

@felixarntz
Copy link
Member Author

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants