Media: apply the `wp_editor_set_quality` filter not only when loading an image in the editor but also when saving an converted image, after the mime-type of the output image has changed.

Props mikeschroder, desrosj, azaozz.
Fixes #53667.
Built from https://develop.svn.wordpress.org/trunk@51704


git-svn-id: http://core.svn.wordpress.org/trunk@51310 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2021-08-31 18:59:03 +00:00
parent 4112d77597
commit f405add8f0
2 changed files with 50 additions and 6 deletions

View File

@ -15,9 +15,12 @@ abstract class WP_Image_Editor {
protected $file = null; protected $file = null;
protected $size = null; protected $size = null;
protected $mime_type = null; protected $mime_type = null;
protected $output_mime_type = null;
protected $default_mime_type = 'image/jpeg'; protected $default_mime_type = 'image/jpeg';
protected $quality = false; protected $quality = false;
protected $default_quality = 82;
// Deprecated since 5.8.1. See get_default_quality() below.
protected $default_quality = 82;
/** /**
* Each instance handles a single file. * Each instance handles a single file.
@ -224,6 +227,11 @@ abstract class WP_Image_Editor {
* @return true|WP_Error True if set successfully; WP_Error on failure. * @return true|WP_Error True if set successfully; WP_Error on failure.
*/ */
public function set_quality( $quality = null ) { public function set_quality( $quality = null ) {
// Use the output mime type if present. If not, fall back to the input/initial mime type.
$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
// Get the default quality setting for the mime type.
$default_quality = $this->get_default_quality( $mime_type );
if ( null === $quality ) { if ( null === $quality ) {
/** /**
* Filters the default image compression quality setting. * Filters the default image compression quality setting.
@ -238,9 +246,9 @@ abstract class WP_Image_Editor {
* @param int $quality Quality level between 1 (low) and 100 (high). * @param int $quality Quality level between 1 (low) and 100 (high).
* @param string $mime_type Image mime type. * @param string $mime_type Image mime type.
*/ */
$quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type ); $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
if ( 'image/jpeg' === $this->mime_type ) { if ( 'image/jpeg' === $mime_type ) {
/** /**
* Filters the JPEG compression quality for backward-compatibility. * Filters the JPEG compression quality for backward-compatibility.
* *
@ -261,7 +269,7 @@ abstract class WP_Image_Editor {
} }
if ( $quality < 0 || $quality > 100 ) { if ( $quality < 0 || $quality > 100 ) {
$quality = $this->default_quality; $quality = $default_quality;
} }
} }
@ -278,6 +286,27 @@ abstract class WP_Image_Editor {
} }
} }
/**
* Returns the default compression quality setting for the mime type.
*
* @since 5.8.1
*
* @param string $mime_type
* @return int The default quality setting for the mime type.
*/
protected function get_default_quality( $mime_type ) {
switch ( $mime_type ) {
case 'image/webp':
$quality = 86;
break;
case 'image/jpeg':
default:
$quality = $this->default_quality;
}
return $quality;
}
/** /**
* Returns preferred mime-type and extension based on provided * Returns preferred mime-type and extension based on provided
* file's extension and mime, or current file's extension and mime. * file's extension and mime, or current file's extension and mime.
@ -374,13 +403,28 @@ abstract class WP_Image_Editor {
$new_ext = $this->get_extension( $mime_type ); $new_ext = $this->get_extension( $mime_type );
} }
if ( $filename ) { // Ensure both $filename and $new_ext are not empty.
// $this->get_extension() returns false on error which would effectively remove the extension
// from $filename. That shouldn't happen, files without extensions are not supported.
if ( $filename && $new_ext ) {
$dir = pathinfo( $filename, PATHINFO_DIRNAME ); $dir = pathinfo( $filename, PATHINFO_DIRNAME );
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); $ext = pathinfo( $filename, PATHINFO_EXTENSION );
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
} }
if ( $mime_type && ( $mime_type !== $this->mime_type ) ) {
// The image will be converted when saving. Set the quality for the new mime-type if not already set.
if ( $mime_type !== $this->output_mime_type ) {
$this->output_mime_type = $mime_type;
$this->set_quality();
}
} elseif ( ! empty( $this->output_mime_type ) ) {
// Reset output_mime_type and quality.
$this->output_mime_type = null;
$this->set_quality();
}
return array( $filename, $new_ext, $mime_type ); return array( $filename, $new_ext, $mime_type );
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-51703'; $wp_version = '5.9-alpha-51704';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.