Make WordPress Core

Changeset 57607


Ignore:
Timestamp:
02/12/2024 11:24:19 PM (4 months ago)
Author:
adamsilverstein
Message:

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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r57524 r57607  
    503503        if ( ! $filename ) {
    504504            $filename = $this->generate_filename( null, null, $extension );
     505        }
     506
     507        if ( function_exists( 'imageinterlace' ) ) {
     508            /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
     509            imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) );
    505510        }
    506511
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r57524 r57607  
    490490                }
    491491            }
    492 
    493             if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) {
    494                 $this->image->setInterlaceScheme( Imagick::INTERLACE_NO );
    495             }
    496492        } catch ( Exception $e ) {
    497493            return new WP_Error( 'image_resize_error', $e->getMessage() );
     
    826822        }
    827823
     824        if ( method_exists( $this->image, 'setInterlaceScheme' ) && method_exists( $this->image, 'getInterlaceScheme' ) && defined( 'Imagick::INTERLACE_PLANE' ) ) {
     825            $orig_interlace = $this->image->getInterlaceScheme();
     826            /**
     827             * Filters whether to output progressive images (if available).
     828             *
     829             * @since 6.5.0
     830             *
     831             * @param bool   $interlace Whether to use progressive images for output if available. Default false.
     832             * @param string $mime_type The mime type being saved.
     833             */
     834            if ( apply_filters( 'image_save_progressive', false, $mime_type ) ) {
     835                $this->image->setInterlaceScheme( Imagick::INTERLACE_PLANE ); // True - line interlace output.
     836            } else {
     837                $this->image->setInterlaceScheme( Imagick::INTERLACE_NO ); // False - no interlace output.
     838            }
     839        }
     840
    828841        $write_image_result = $this->write_image( $this->image, $filename );
    829842        if ( is_wp_error( $write_image_result ) ) {
     
    834847            // Reset original format.
    835848            $this->image->setImageFormat( $orig_format );
     849            if ( isset( $orig_interlace ) ) {
     850                $this->image->setInterlaceScheme( $orig_interlace );
     851            }
    836852        } catch ( Exception $e ) {
    837853            return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
Note: See TracChangeset for help on using the changeset viewer.