Skip to content

Commit

Permalink
Media: enable control of progressive image output.
Browse files Browse the repository at this point in the history
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.



git-svn-id: https://develop.svn.wordpress.org/trunk@57607 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
adamsilverstein committed Feb 12, 2024
1 parent 4f2965e commit 26f8360
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/wp-includes/class-wp-image-editor-gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
$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' ) );
Expand Down
24 changes: 20 additions & 4 deletions src/wp-includes/class-wp-image-editor-imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,6 @@ protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIAN
$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() );
}
Expand Down Expand Up @@ -825,6 +821,23 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
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;
Expand All @@ -833,6 +846,9 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
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 );
}
Expand Down

0 comments on commit 26f8360

Please sign in to comment.