diff --git src/wp-includes/class-wp-image-editor.php src/wp-includes/class-wp-image-editor.php
index 7dcdc91d5a..d322d6a7cb 100644
|
|
abstract class WP_Image_Editor { |
316 | 316 | $new_ext = $file_ext; |
317 | 317 | } |
318 | 318 | |
| 319 | /** |
| 320 | * Filters the image editor output format mapping. |
| 321 | * |
| 322 | * Enables filtering the mime type used to save images. By default, |
| 323 | * the mapping array is empty, so the mime type matches the source image. |
| 324 | * |
| 325 | * @see src/wp-includes/class-wp-image-editor.php -> get_output_format() |
| 326 | * |
| 327 | * @since 5.8.0 |
| 328 | * |
| 329 | * @param array $wp_image_editor_output_format { |
| 330 | * An array of mime type mappings. Maps a source mime type to a new |
| 331 | * destination mime type. Empty by default. |
| 332 | * |
| 333 | * @type array $mime_type The source mime type { |
| 334 | * @type string $mime_type The new mime type. |
| 335 | * } |
| 336 | * @param string $filename Path to the image. |
| 337 | * @param string $mime_type The source image mime type. |
| 338 | * } |
| 339 | */ |
| 340 | $wp_image_editor_output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type ); |
| 341 | |
| 342 | if ( |
| 343 | isset( $wp_image_editor_output_format[ $mime_type ] ) && |
| 344 | $this->supports_mime_type( $wp_image_editor_output_format[ $mime_type ] ) |
| 345 | ) { |
| 346 | $mime_type = $wp_image_editor_output_format[ $mime_type ]; |
| 347 | $new_ext = $this->get_extension( $mime_type ); |
| 348 | } |
| 349 | |
319 | 350 | // Double-check that the mime-type selected is supported by the editor. |
320 | 351 | // If not, choose a default instead. |
321 | 352 | if ( ! $this->supports_mime_type( $mime_type ) ) { |
diff --git tests/phpunit/tests/image/intermediateSize.php tests/phpunit/tests/image/intermediateSize.php
index b92c833a9d..28ccb220a6 100644
|
|
class Tests_Image_Intermediate_Size extends WP_UnitTestCase { |
62 | 62 | unlink( DIR_TESTDATA . '/images/a2-small-100x75.jpg' ); |
63 | 63 | } |
64 | 64 | |
| 65 | /** |
| 66 | * @requires function imagejpeg |
| 67 | * @ticket 52867 |
| 68 | */ |
| 69 | function test_image_editor_output_format_filter() { |
| 70 | add_filter( |
| 71 | 'image_editor_output_format', |
| 72 | function() { |
| 73 | return array( 'image/jpeg' => 'image/webp' ); |
| 74 | } |
| 75 | ); |
| 76 | $file = DIR_TESTDATA . '/images/waffles.jpg'; |
| 77 | $image = image_make_intermediate_size( $file, 100, 75, true ); |
| 78 | $editor = wp_get_image_editor( $file ); |
| 79 | if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) { |
| 80 | $this->assertSame( 'image/jpeg', $image['mime-type'] ); |
| 81 | } else { |
| 82 | $this->assertSame( 'image/webp', $image['mime-type'] ); |
| 83 | } |
| 84 | unlink( DIR_TESTDATA . '/images/' . $image['file'] ); |
| 85 | remove_all_filters( 'image_editor_output_format' ); |
| 86 | } |
| 87 | |
65 | 88 | /** |
66 | 89 | * @ticket 17626 |
67 | 90 | * @requires function imagejpeg |