From 8284a1f6ae3b6ea8f5ca8abbd537aae976620d1c Mon Sep 17 00:00:00 2001 From: antpb Date: Thu, 20 May 2021 22:15:56 +0000 Subject: [PATCH] Media: Introduces `image_editor_output_format` filter for setting default MIME type of sub size image output. This change introduces the `image_editor_output_format` filter, which fires as sub size images are generated allowing to define a default image MIME type for those items. Props adamsilverstein, williampatton, spacedmonkey, mikeschroder, hellofromTonya, justinahinon, Boniu91, antpb, SergeyBiryukov. Fixes #52867. Built from https://develop.svn.wordpress.org/trunk@50943 git-svn-id: http://core.svn.wordpress.org/trunk@50552 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-image-editor.php | 31 +++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/wp-includes/class-wp-image-editor.php b/wp-includes/class-wp-image-editor.php index 7dcdc91d5a..d322d6a7cb 100644 --- a/wp-includes/class-wp-image-editor.php +++ b/wp-includes/class-wp-image-editor.php @@ -316,6 +316,37 @@ abstract class WP_Image_Editor { $new_ext = $file_ext; } + /** + * Filters the image editor output format mapping. + * + * Enables filtering the mime type used to save images. By default, + * the mapping array is empty, so the mime type matches the source image. + * + * @see src/wp-includes/class-wp-image-editor.php -> get_output_format() + * + * @since 5.8.0 + * + * @param array $wp_image_editor_output_format { + * An array of mime type mappings. Maps a source mime type to a new + * destination mime type. Empty by default. + * + * @type array $mime_type The source mime type { + * @type string $mime_type The new mime type. + * } + * @param string $filename Path to the image. + * @param string $mime_type The source image mime type. + * } + */ + $wp_image_editor_output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type ); + + if ( + isset( $wp_image_editor_output_format[ $mime_type ] ) && + $this->supports_mime_type( $wp_image_editor_output_format[ $mime_type ] ) + ) { + $mime_type = $wp_image_editor_output_format[ $mime_type ]; + $new_ext = $this->get_extension( $mime_type ); + } + // Double-check that the mime-type selected is supported by the editor. // If not, choose a default instead. if ( ! $this->supports_mime_type( $mime_type ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index d1529ce553..590543e20a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50942'; +$wp_version = '5.8-alpha-50943'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.