Media: Output WebP by default when uploading JPEGs.

Uploaded JPEGs will automatically be converted to WebP sub-sizes instead of JPEG, saving space and making sites faster.

The original JPEG upload is always retained and can be accessed by calling `wp_get_original_image_url`.

Props azaozz, flixos90.
Fixes #55443.


Built from https://develop.svn.wordpress.org/trunk@54086


git-svn-id: http://core.svn.wordpress.org/trunk@53645 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2022-09-06 21:15:08 +00:00
parent 4b5968aa9b
commit 897a9743be
5 changed files with 50 additions and 10 deletions

View File

@ -37,6 +37,8 @@ add_filter( 'media_upload_library', 'media_upload_library' );
add_filter( 'media_upload_tabs', 'update_gallery_tab' );
add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
// Admin color schemes.
add_action( 'admin_init', 'register_admin_color_schemes', 1 );
add_action( 'admin_head', 'wp_color_scheme_settings' );

View File

@ -917,10 +917,12 @@ function wp_save_image( $post_id ) {
}
// Save the full-size file, also needed to create sub-sizes.
if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
$saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
if ( ! $saved ) {
$return->error = esc_js( __( 'Unable to save the image.' ) );
return $return;
}
$new_path = $saved['path'];
if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
$tag = false;

View File

@ -3843,3 +3843,18 @@ function wp_media_attach_action( $parent_id, $action = 'attach' ) {
exit;
}
}
/**
* Filters the default image output mapping.
*
* With this filter callback, WebP image files will be generated for certain JPEG source files.
*
* @since 6.1.0
*
* @param array $output_mapping Map of mime type to output format.
* @retun array The adjusted default output mapping.
*/
function wp_default_image_output_mapping( $output_mapping ) {
$output_mapping['image/jpeg'] = 'image/webp';
return $output_mapping;
}

View File

@ -424,19 +424,26 @@ abstract class WP_Image_Editor {
return array( $filename, $new_ext, $mime_type );
}
/**
* Builds an output filename based on current file, and adding proper suffix
/**
* Builds an output filename based on current file, and adding proper suffix.
*
* @since 3.5.0
* @since 6.1.0 Skips adding a suffix when set to an empty string. When the
* file extension being generated doesn't match the image file extension,
* add the extension to the suffix
*
* @param string $suffix
* @param string $dest_path
* @param string $extension
* @return string filename
* @param string $suffix Optional. Suffix to add to the filename. The default null
* will result in a 'widthxheight' suffix. Passing
* an empty string will result in no suffix.
* @param string $dest_path Optional. The path to save the file to. The default null
* will use the image file path.
* @param string $extension Optional. The file extension to use. The default null
* will use the image file extension.
* @return string filename The generated file name.
*/
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
// $suffix will be appended to the destination filename, just before the extension.
if ( ! $suffix ) {
if ( null === $suffix ) {
$suffix = $this->get_suffix();
}
@ -457,7 +464,21 @@ abstract class WP_Image_Editor {
}
}
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
if ( empty( $suffix ) ) {
$suffix = '';
} else {
$suffix = "-{$suffix}";
}
// When the file extension being generated doesn't match the image file extension,
// add the extension to the suffix to ensure a unique file name. Prevents
// name conflicts when a single image type can have multiple extensions,
// eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
if ( ! empty( $extension ) && $extension !== $ext ) {
$suffix .= "-{$ext}";
}
return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-54085';
$wp_version = '6.1-alpha-54086';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.