WordPress.org

Make WordPress Core

Opened 5 weeks ago

Last modified 31 hours ago

#52867 new enhancement

Add capability to set default format for image sub-sizes.

Reported by: adamsilverstein Owned by:
Milestone: 5.8 Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch has-unit-tests dev-feedback
Focuses: Cc:

Description (last modified by adamsilverstein)

Introduces a new filter that enables setting a default sub-size image output format when uploading images.

This enables testing WebP (or other image formats) as the default output format.

Follow up to #35725 where we introduced support for WebP images.

By switching to WebP as their default output format, users won't need to do anything and will benefit from faster sites because images will be 25-40% smaller, with no loss of quality. Hosts will benefit from reduced memory usage for compression, and reduced storage and bandwidth requirements for images.

By using WebP as the default image format:

  • Uploaded images are converted to smaller sub-sizes - these images will now use the WebP format if the server supports it.
    • Note both GD and Imagick support WebP lossy and lossless, however only Imagick supports animated images.

Considerations:

  • We need to research the best compression level to use for WebP. In particular, we need to pick a compression level that results in images that are very close to the images our current compression defaults produce. Some details on testing methodology, plus a link to research with a set of test images we can use in this comment.
  • Some browsers still don't support WebP, so we should consider providing a https://github.com/chase-moskal/webp-hero shim] (behind feature detection)for sites that need to support these browsers, or whether letting these sites opt out is sufficient.
  • Disabling the feature is a one line code change, still we should release a plugin non-technical users can install to disable the feature.
  • Testing, testing, testing!

Attachments (3)

52867.diff (14.3 KB) - added by adamsilverstein 3 weeks ago.
52867.2.diff (1.5 KB) - added by adamsilverstein 3 weeks ago.
52867.3.diff (1.6 KB) - added by adamsilverstein 3 weeks ago.

Download all attachments as: .zip

Change History (20)

This ticket was mentioned in PR #1108 on WordPress/wordpress-develop by adamsilverstein.


5 weeks ago

  • Keywords has-patch has-unit-tests added

#2 @adamsilverstein
5 weeks ago

  • Summary changed from Use WebP as default format for image subsizes (when available). to Use WebP as default format for image sub-sizes (when available).

#3 @SergeyBiryukov
5 weeks ago

  • Description modified (diff)

#4 @williampatton
5 weeks ago

Just noting that I discovered Safari supports .webp images as of MacOS 11 Big Sur and later only. https://caniuse.com/webp

#5 @adamsilverstein
4 weeks ago

Just noting that I discovered Safari supports .webp images as of MacOS 11 Big Sur and later only. https://caniuse.com/webp

Good point - users on older OS X would rely on the shim. Sites or hosts who expect a higher than usual percentage of non supported browsers can opt out with a single line of code (or a plugin).

#7 @adamsilverstein
3 weeks ago

52867.2.diff is the (renamed) filter part only of this patch. This filters lets developers set the default output format (mime type) and this would enable creating a simple plugin users could run to enable WebP (or any other format) as their default output format. Naming suggestions welcome!

Having a plugin available would enable much wider testing of the feature.

Testing:

To save uploaded jpeg images as WebP by default, add this to your codebase:

add_filter( 'wp_image_editor_output_format', function( $formats ) {
	$formats['image/jpg'] = 'image/webp';
	return $formats;
}

#8 @adamsilverstein
3 weeks ago

  • Keywords dev-feedback added

@mikeschroder @spacedmonkey how do you feel about 52867.2.diff which adds a new simplified wp_image_editor_output_format filter (naming suggestions welcome!) in WP_Image_Editor::get_output_format so we can build a feature plugin that enables wider testing of the "WebP as default output format" feature.

Furthermore, with this filter in place we could build a "Modern image formats tester" feature plugin that would let users enable other advanced formats (AVIF, JPEX XL) when those formats are available.

Last edited 3 weeks ago by adamsilverstein (previous) (diff)

#9 @spacedmonkey
3 weeks ago

Question, what happens if an animated gif is uploaded? Can PHP convert animated gifs into animated WEBPs?

#10 @spacedmonkey
3 weeks ago

So once merged, this would generates image sub sizes as webps right?

I personally believe that if we do this, we need an add an option on wp-admin/options-media.php
setting page to allow users to opt out of it.

#11 @adamsilverstein
3 weeks ago

Question, what happens if an animated gif is uploaded? Can PHP convert animated gifs into animated WEBPs?

If the server image engine supports animated WebP GIFs can get converted to WebP. In general GD does not support animation, only Imagick.

So once merged, this would generates image sub sizes as webps right?

No, for now this only adds a filter that gives use the capability to set the default output type (given a specific input type). In any case, the specified target is only used if the system supports it.

I created a simple plugin for testing: https://github.com/adamsilverstein/wordpress-modern-images/. With the patch here applied, install this plugin, then choose your preferred output format under Settings->Media (currently only alters JPEG uploads). Try setting this to WebP and uploading a JPEG image to a post. The image sub-sizes used should all be WebP if the system supports it.

I personally believe that if we do this, we need an add an option on wp-admin/options-media.php

setting page to allow users to opt out of it.

I'm proposing a feature plugin first, so users can test this feature. I agree users need a way to opt out, although I'm not certain about the UI.

#12 @adamsilverstein
3 weeks ago

  • Description modified (diff)
  • Summary changed from Use WebP as default format for image sub-sizes (when available). to Add capability to set default format for image sub-sizes.

#13 @adamsilverstein
3 weeks ago

@spacedmonkey I updated the ticket title/description to make it clear the goal here is introduce only the capability so far, not change output. I will move the final part about making WebP the default format into another ticket.

#14 @adamsilverstein
2 days ago

  • Milestone changed from Future Release to 5.8

@spacedmonkey, @desrosj, @mikeschroder - can you give this (filter only) patch a review (PR)? I'd love to land this in 5.8 as well as it enables building a plugin with the "WebP as default output format" feature.

I can work to add some basic tests for the filter.

#15 @mikeschroder
37 hours ago

@adamsilverstein I like the idea of adding a filter, thank you!

I'll give it a review + test.
Agreed that a test for the filter is a good idea.

Wanted to also add -- I think it's fine and perhaps helpful to continue to have a ticket (whether this one or a different one) for the proposed feature so that discussion can continue, even if it ends up becoming a feature project / plugin.

#16 @mikeschroder
37 hours ago

Just tested, and this looks to work pretty well, thank you!

Two notes:

  • I noticed that the default image editor on my environment, Imagick, did not support WebP. GD on the same system did support WebP, but it wasn't used because the editor is chosen on whether it supports the format being loaded. This is an existing problem, really -- but figured it was worth mentioning.
  • I wonder if we should name this filter image_editor_output_format to line up with the existing image_editor_default_mime_type and image_editor_save_pre?

#17 @spacedmonkey
31 hours ago

Wondering it is would be cleaner to have a filter like this.

return apply_filters( 'image_editor_output_format', array( $filename, $new_ext, $mime_type ) );

Just filter return value of get_output_format method. Feels a lot more flexible.

Note: See TracTickets for help on using tickets.