Media: improve image engine detection when using the output format filter.

When the output format is altered with the `image_editor_output_format` filter, prefer the image engine that supports both input an output types, falling back to the engine that supports the input type.

Correct an issue where the output format filter wasn't respected because the selected engine didn't support the output format.

Props mikeschroder, ironprogrammer.
Fixes #54476.



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


git-svn-id: http://core.svn.wordpress.org/trunk@53975 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2022-10-07 19:03:11 +00:00
parent 59b066ef11
commit 2d9ac13f69
2 changed files with 29 additions and 2 deletions

View File

@ -3838,6 +3838,7 @@ function wp_max_upload_size() {
function wp_get_image_editor( $path, $args = array() ) {
$args['path'] = $path;
// If the mime type is not set in args, try to extract and set it from the file.
if ( ! isset( $args['mime_type'] ) ) {
$file_info = wp_check_filetype( $args['path'] );
@ -3848,6 +3849,15 @@ function wp_get_image_editor( $path, $args = array() ) {
}
}
// Check and set the output mime type mapped to the input type.
if ( isset( $args['mime_type'] ) ) {
/** This filter is documented in wp-includes/class-wp-image-editor.php */
$output_format = apply_filters( 'image_editor_output_format', array(), $path, $args['mime_type'] );
if ( isset( $output_format[ $args['mime_type'] ] ) ) {
$args['output_mime_type'] = $output_format[ $args['mime_type'] ];
}
}
$implementation = _wp_image_editor_choose( $args );
if ( $implementation ) {
@ -3900,12 +3910,14 @@ function _wp_image_editor_choose( $args = array() ) {
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
*/
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
$supports_input = false;
foreach ( $implementations as $implementation ) {
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
continue;
}
// Implementation should support the passed mime type.
if ( isset( $args['mime_type'] ) &&
! call_user_func(
array( $implementation, 'supports_mime_type' ),
@ -3914,16 +3926,31 @@ function _wp_image_editor_choose( $args = array() ) {
continue;
}
// Implementation should support requested methods.
if ( isset( $args['methods'] ) &&
array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
continue;
}
// Implementation should ideally support the output mime type as well if set and different than the passed type.
if (
isset( $args['mime_type'] ) &&
isset( $args['output_mime_type'] ) &&
$args['mime_type'] !== $args['output_mime_type'] &&
! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_type'] )
) {
// This implementation supports the imput type but not the output type.
// Keep looking to see if we can find an implementation that supports both.
$supports_input = $implementation;
continue;
}
// Favor the implementation that supports both input and output mime types.
return $implementation;
}
return false;
return $supports_input;
}
/**

View File

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