Make WordPress Core

#56526 closed defect (bug) (invalid)

Generate WebP for JPEG based on image size

Reported by: flixos90's profile flixos90 Owned by: flixos90's profile flixos90
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch has-unit-tests
Focuses: Cc:


Follow up to #55443: While the multi MIME type architecture has been reverted, part of that code that came as an iteration to the original proposal was to allow fine tuned control over which image sizes should use WebP vs JPEG.

For the vast majority of sizes, it will make sense to use WebP (e.g. any size that is used in the frontend). However, there are some special cases we need to consider, e.g. sizes that are registered only for a special circumstance, such as usage in an email newsletter or in Open Graph tags, or any other technology that may not support WebP.

This ticket is to track this work, and it is a prerequisite to the already opened #56288, which will be used to control this aspect for custom image sizes in a declarative way.

Change History (9)

This ticket was mentioned in PR #3166 on WordPress/wordpress-develop by felixarntz.

21 months ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

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:

### 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.
  • Still to do: Add test coverage.

#2 @flixos90
21 months ago

In 54094:

Media: Move wp_default_image_output_mapping() filter callback to frontend scope.

While the image_editor_output_format filter is primarily used in WP Admin, it can also be executed in frontend scope, as the related WP_Image_Editor class and wp_unique_filename() function are being loaded in that scope.

Follow up to [54086].

See #55443, #56526.

#3 @flixos90
21 months ago

  • Keywords commit added

felixarntz commented on PR #3166:

21 months ago

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

#5 @adamsilverstein
21 months ago

Looks good, nice work!

#6 @flixos90
21 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 54097:

Media: Generate WebP only for certain registered image sizes.

The existing filter image_editor_output_format receives an additional parameter $size_name which is populated whenever it controls the output format for a specific registered image size to create. Otherwise, it remains empty. In order to achieve this, a low level change has been added in bringing a new $size_name class property to the WP_Image_Editor base class, which is introduced in a backward compatible way that will not cause conflicts with custom implementations.

This parameter is then used in new logic inside the wp_default_image_output_mapping() callback function for the filter, controlling whether image/jpeg should map to image/webp output or not. By default, this is enabled for all WordPress core image sizes by default, and this list can be modified using a new wp_image_sizes_with_additional_mime_type_support filter, e.g. to remove core sizes or add custom sizes.

The customization per image size may be further enhanced by providing a more declarative API via a new parameter on the add_image_size() function.

Props eugenemanuilov, flixos90, adamsilverstein, joegrainger.

Fixes #56526.
See #55443, #56288.

#8 @flixos90
21 months ago

  • Keywords needs-dev-note added; commit removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

#9 @davidbaumwald
20 months ago

  • Keywords needs-dev-note removed
  • Milestone 6.1 deleted
  • Resolution set to invalid
  • Status changed from reopened to closed

This was reverted in r54226, so it won't need a Dev Note for 6.1. @flixos90 confirmed this should move to Future Release for continued work.

Note: See TracTickets for help on using tickets.