From a843b188f8e5ca5c42bd28afe00150bc4cebff06 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 12 Feb 2024 23:26:11 +0000 Subject: [PATCH] Media: enable control of progressive image output. Add a new `image_save_progressive` filter which developers can use to control whether intermediate image sizes are saved in a progressive format (when available). By default, progressive image output is not used, matching the previous behavior. Props: adamsilverstein, _ck_, markoheijnen, SergeyBiryukov, Japh, pmeenan, mikeschroder, derekspringer, buley, ericlewis, bahia0019, born2webdesign. Fixes #21668. Built from https://develop.svn.wordpress.org/trunk@57607 git-svn-id: http://core.svn.wordpress.org/trunk@57108 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-image-editor-gd.php | 5 ++++ wp-includes/class-wp-image-editor-imagick.php | 24 +++++++++++++++---- wp-includes/version.php | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/wp-includes/class-wp-image-editor-gd.php b/wp-includes/class-wp-image-editor-gd.php index 23331c7f19..938ae61d48 100644 --- a/wp-includes/class-wp-image-editor-gd.php +++ b/wp-includes/class-wp-image-editor-gd.php @@ -504,6 +504,11 @@ class WP_Image_Editor_GD extends WP_Image_Editor { $filename = $this->generate_filename( null, null, $extension ); } + if ( function_exists( 'imageinterlace' ) ) { + /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */ + imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) ); + } + if ( 'image/gif' === $mime_type ) { if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php index 546ad3e799..64babdad5a 100644 --- a/wp-includes/class-wp-image-editor-imagick.php +++ b/wp-includes/class-wp-image-editor-imagick.php @@ -489,10 +489,6 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { $this->image->setImageDepth( 8 ); } } - - if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) { - $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); - } } catch ( Exception $e ) { return new WP_Error( 'image_resize_error', $e->getMessage() ); } @@ -825,6 +821,23 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); } + if ( method_exists( $this->image, 'setInterlaceScheme' ) && method_exists( $this->image, 'getInterlaceScheme' ) && defined( 'Imagick::INTERLACE_PLANE' ) ) { + $orig_interlace = $this->image->getInterlaceScheme(); + /** + * Filters whether to output progressive images (if available). + * + * @since 6.5.0 + * + * @param bool $interlace Whether to use progressive images for output if available. Default false. + * @param string $mime_type The mime type being saved. + */ + if ( apply_filters( 'image_save_progressive', false, $mime_type ) ) { + $this->image->setInterlaceScheme( Imagick::INTERLACE_PLANE ); // True - line interlace output. + } else { + $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); // False - no interlace output. + } + } + $write_image_result = $this->write_image( $this->image, $filename ); if ( is_wp_error( $write_image_result ) ) { return $write_image_result; @@ -833,6 +846,9 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { try { // Reset original format. $this->image->setImageFormat( $orig_format ); + if ( isset( $orig_interlace ) ) { + $this->image->setInterlaceScheme( $orig_interlace ); + } } catch ( Exception $e ) { return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index e884879323..02330a5545 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57606'; +$wp_version = '6.5-alpha-57607'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.