WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 3 months ago

#52867 closed enhancement

Add capability to set default format for image sub-sizes. — at Version 12

Reported by: adamsilverstein Owned by:
Milestone: 5.8 Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch dev-feedback has-unit-tests needs-testing has-testing-info needs-docs needs-dev-note
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!

Change History (14)

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


7 months ago

  • Keywords has-patch has-unit-tests added

#2 @adamsilverstein
7 months 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
7 months ago

  • Description modified (diff)

#4 @williampatton
7 months 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
7 months 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
7 months 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
7 months 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 7 months ago by adamsilverstein (previous) (diff)

#9 @spacedmonkey
7 months ago

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

#10 @spacedmonkey
7 months 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
7 months 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
7 months 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.
Note: See TracTickets for help on using tickets.