Media: improve filter to enable setting output quality by image size.

Add a new $size parameter to the  wp_editor_set_quality filter. $size is an array with 'width' and 'height' keys. Developers can use this information to set image quality based on the image size.

Props adamsilverstein, joemcgill, Mte90, codekraft, birgire, azaozz, sppramodh.
Fixes #54648.



Built from https://develop.svn.wordpress.org/trunk@59473


git-svn-id: http://core.svn.wordpress.org/trunk@58859 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2024-11-29 23:48:15 +00:00
parent 47bb776fd7
commit c08c2e28cc
4 changed files with 41 additions and 11 deletions

View File

@ -221,6 +221,14 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
$this->set_quality(
null,
array(
'width' => $dst_w,
'height' => $dst_h,
)
);
$resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
@ -568,12 +576,14 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
* Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
*
* @since 6.7.0
* @since 6.8.0 The `$dims` parameter was added.
*
* @param int $quality Compression Quality. Range: [1,100]
* @param int $quality Compression Quality. Range: [1,100]
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
* @return true|WP_Error True if set successfully; WP_Error on failure.
*/
public function set_quality( $quality = null ) {
$quality_result = parent::set_quality( $quality );
public function set_quality( $quality = null, $dims = array() ) {
$quality_result = parent::set_quality( $quality, $dims );
if ( is_wp_error( $quality_result ) ) {
return $quality_result;
} else {
@ -586,7 +596,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
$webp_info = wp_get_webp_info( $this->file );
if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
$quality = IMG_WEBP_LOSSLESS;
parent::set_quality( $quality );
parent::set_quality( $quality, $dims );
}
}
} catch ( Exception $e ) {

View File

@ -190,12 +190,14 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
* Sets Image Compression quality on a 1-100% scale.
*
* @since 3.5.0
* @since 6.8.0 The `$dims` parameter was added.
*
* @param int $quality Compression Quality. Range: [1,100]
* @param int $quality Compression Quality. Range: [1,100]
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
* @return true|WP_Error True if set successfully; WP_Error on failure.
*/
public function set_quality( $quality = null ) {
$quality_result = parent::set_quality( $quality );
public function set_quality( $quality = null, $dims = array() ) {
$quality_result = parent::set_quality( $quality, $dims );
if ( is_wp_error( $quality_result ) ) {
return $quality_result;
} else {
@ -367,6 +369,14 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
}
$this->set_quality(
null,
array(
'width' => $dst_w,
'height' => $dst_h,
)
);
// Execute the resize.
$thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
if ( is_wp_error( $thumb_result ) ) {

View File

@ -240,11 +240,14 @@ abstract class WP_Image_Editor {
* Sets Image Compression quality on a 1-100% scale.
*
* @since 3.5.0
* @since 6.8.0 The `$dims` parameter was added.
*
* @param int $quality Compression Quality. Range: [1,100]
* @param int $quality Compression Quality. Range: [1,100]
* @param array $dims Optional. Image dimensions array with 'width' and 'height' keys.
* @return true|WP_Error True if set successfully; WP_Error on failure.
*/
public function set_quality( $quality = null ) {
public function set_quality( $quality = null, $dims = array() ) {
// 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.
@ -260,11 +263,18 @@ abstract class WP_Image_Editor {
* The WP_Image_Editor::set_quality() method has priority over the filter.
*
* @since 3.5.0
* @since 6.8.0 Added the size parameter.
*
* @param int $quality Quality level between 1 (low) and 100 (high).
* @param string $mime_type Image mime type.
* @param array $size {
* Dimensions of the image.
*
* @type int $width The image width.
* @type int $height The image height.
* }
*/
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );
if ( 'image/jpeg' === $mime_type ) {
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59472';
$wp_version = '6.8-alpha-59473';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.